Command Line Shell Programming Part 2
Command Line Shell Programming Part 2
Command-Line Arguments
The command-line arguments $1, $2, $3, ...$9 are positional parameters, with $0 pointing to the actual command,
program, shell script, or function and $1, $2, $3, ...$9 as the arguments to the command.
Following script uses various special variables related to the command line −
#!/bin/sh
Both the parameters specify the command-line arguments. However, the "$*" special parameter takes the entire
list as one argument with spaces between and the "$@" special parameter takes the entire list and separates it into
separate arguments.
We can write the shell script as shown below to process an unknown number of commandline arguments with either
the $* or $@ special parameters −
#!/bin/sh
for TOKEN in $*
do
echo $TOKEN
done
Exit Status
The $? variable represents the exit status of the previous command.
Exit status is a numerical value returned by every command upon its completion. As a rule, most commands return
an exit status of 0 if they were successful, and 1 if they were unsuccessful.
Some commands return additional exit statuses for particular reasons. For example, some commands differentiate
between kinds of errors and will return various exit values depending on the specific type of failure.
Escape Characters
Certain characters are significant to the shell; we have seen, for example, that the use of double quotes (")
characters affect how spaces and TAB characters are treated, for example:
$ echo Hello World
Hello World
$ echo "Hello World"
Hello World
So how do we display: Hello "World" ?