Open in CoCalc with one click!

Jan's comments

On flags and arguments

Bash parses the command line input into words. It executes the first word and passes the rest of them to it as an array. Its up to the program to process this array. Most programs will accept an undefined number of arguments. It is pretty standard that the flags need to come before the arguments.

It is customary to group short options and to interpret every argument that starts with a dash as an option. Short flags start with a - and can be combined while long flags start with -- and cannot. In the case of a sequence of files that needs to be explicitly marked, it is also customary to insert -- before the list to signal that no options will follow.

Most programs with a simple command line syntax use the getopt(3) function in the C library on Unix. This function handles basic parsing and behaves as described above.

Excercise

With a basic understanding of Bash and programming, you should be able to explain why there can not be less whitespace in the following (except for the cosmetic ones after ;):

if [ "$USER" == "$(id -nu)" ]; then echo tautology; fi

A frustrating Windows example

The following code works in the DOS shell, but not on Unix.

cd..

On Windows, they probably made a special case or they do not treat a dot as a part of a word. A Unix shell tokenises according to whitespace. In this case, it would try to run a program called cd.., which does not exist.

What's really going to bake your noodle later on: you can not write a program like that, because the current working directory is a runtime attribute of a process. Such a program could only change its own current working directory and not also its parent shell's. After the program terminated, the shell would return you to the directory in which you started the program. All paths (except those starting with /) are resolved with respect to the current working directory.