Читать книгу Linux Bible - Christopher Negus - Страница 200
Special shell positional parameters
ОглавлениеThere are special variables that the shell assigns for you. One set of commonly used variables is called positional parameters or command-line arguments, and it is referenced as $0, $1, $2, $3…$n. $0 is special, and it is assigned the name used to invoke your script; the others are assigned the values of the parameters passed on the command line in the order they appeared. For instance, let's say that you had a shell script named myscript
which contained the following:
#!/bin/bash # Script to echo out command-line arguments echo "The first argument is $1, the second is $2." echo "The command itself is called $0." echo "There are $# parameters on your command line" echo "Here are all the arguments: $@"
Assuming that the script is executable and located in a directory in your $PATH
, the following shows what would happen if you ran that command with foo
and bar
as arguments:
$ chmod 755 /home/chris/bin/myscript $ myscript foo bar The first argument is foo, the second is bar. The command itself is called /home/chris/bin/myscript. There are 2 parameters on your command line Here are all the arguments: foo bar
As you can see, the positional parameter $0
is the full path or relative path to myscript
, $1
is foo
, and $2
is bar
.
Another variable, $#
, tells you how many parameters your script was given. In the example, $#
would be 2. The $@
variable holds all of the arguments entered at the command line. Another particularly useful special shell variable is $?
, which receives the exit status of the last command executed. Typically, a value of zero means that the command exited successfully, and anything other than zero indicates an error of some kind. For a complete list of special shell variables, refer to the bash
man page.