I often come across $? $0 $1 $2 etc....
in shell scripting, what I know is that $?
returns the exit status of the last command
echo "this will return 0" echo $?
but what do the others do? what are they called and is there more? perhaps like $3 $4 $5 ...
$2 is the second command-line argument passed to the shell script or function. Also, know as Positional parameters.
If you execute ./script.sh , $0 will give output ./script.sh but if you execute it with bash script.sh it will give output script.sh . Show activity on this post. They are called the Positional Parameters.
$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded. The grep manpage states: The exit status is 0 if selected lines are found, and 1 if not found.
$# Stores the number of command-line arguments that were passed to the shell program. $? Stores the exit value of the last command that was executed. $0 Stores the first word of the entered command (the name of the shell program). $* Stores all the arguments that were entered on the command line ($1 $2 ...).
These are positional arguments of the script.
Executing
./script.sh Hello World
Will make
$0 = ./script.sh $1 = Hello $2 = World
Note
If you execute ./script.sh
, $0
will give output ./script.sh
but if you execute it with bash script.sh
it will give output script.sh
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With