Passing command line arguments through Bash
While brushing on bash (it was a while) I was surprised to notice that the execution of this code is being saved as script.sh:
echo "Arg 0 to script.sh: $0"
echo "Arg 1 to script.sh: $1"
function echo_args
{
echo "Arg 0 to echo_args: $0"
echo "Arg 1 to echo_args: $1"
}
echo_args
like this:
>> ./script.sh argument
output this:
Arg 0 to script.sh: ./script.sh
Arg 1 to script.sh: argument
Arg 0 to echo_args: ./script.sh
Arg 1 to echo_args:
I was surprised to see $ 0 from script.sh passed as $ 0 to echo_args when $ 1 is not handled similarly. It seems to me that it should be both.
Any clarification is appreciated.
+3
Schemer
source
to share
1 answer
$ 0 is a "special parameter" in bash that always evaluates the script name and is set at the beginning of the script. It looks like it also means that it is somewhat global as it cannot be reassigned.
For more information, this is a pretty good recommendation:
http://bash.cyberciti.biz/guide/$0
+3
Jordan robinson
source
to share