Special Variables ($0 $1 $?)
In this page:
Special Variables ($0 $1 $?)
$0 is the script's own name, $1, $2, etc. are positional arguments passed to the script, and $? holds the exit status of the most recently run command. $# gives the number of arguments, and $@ expands to all of them. These special variables are essential for writing scripts that accept input and check for errors.
Note: Always check $? right after the command you care about — it gets overwritten by every subsequent command.
Example: Special Variables ($0 $1 $?)
#!/bin/bash
echo "Script name: $0"
echo "First arg: $1"
echo "Arg count: $#"
true
echo "Exit status of true: $?"
false
echo "Exit status of false: $?"
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: