← Back to Bash Course | Chapter 2: Variables & Types | Lesson 11 of 13

Special Variables ($0 $1 $?)

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 $?)

bash
#!/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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.