0
0
Bash Scriptingscripting~10 mins

Special variables ($0, $1, $#, $@, $?, $) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Special variables ($0, $1, $#, $@, $?, $$)
Start Script
Read $0: Script Name
Read $1: First Argument
Read $# : Number of Arguments
Read $@ : All Arguments
Execute Command
Read $? : Last Command Exit Status
Read $$ : Current Script PID
End Script
The script starts by reading special variables: $0 for script name, $1 for first argument, $# for argument count, $@ for all arguments, $? for last command status, and $$ for script process ID.
Execution Sample
Bash Scripting
#!/bin/bash

# Example script

echo "Script name: $0"
echo "First arg: $1"
echo "Number of args: $#"
echo "All args: $@"
ls
echo "Last command exit status: $?"
echo "Script PID: $$"
This script prints special variables and runs 'ls' to show $? usage.
Execution Table
StepVariable/CommandValue/ActionOutput
1$0./example.shScript name: ./example.sh
2$1helloFirst arg: hello
3$#2Number of args: 2
4$@hello worldAll args: hello world
5lsruns ls commandLists files in current directory
6$?0Last command exit status: 0
7$$12345Script PID: 12345
💡 Script ends after printing all special variables and command output.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
$0unset./example.sh./example.sh./example.sh./example.sh./example.sh./example.sh./example.sh
$1unsethellohellohellohellohellohellohello
$#unset2222222
$@unsethello worldhello worldhello worldhello worldhello worldhello worldhello world
$?unsetunsetunsetunsetunsetunset00
$$unset12345123451234512345123451234512345
Key Moments - 3 Insights
Why does $? show 0 after running ls?
Because $? holds the exit status of the last command. 'ls' succeeded, so $? is 0 as shown in step 6.
What does $@ show compared to $1?
$1 is only the first argument (step 2), but $@ shows all arguments separated by spaces (step 4).
Why is $$ useful?
$$ gives the current script's process ID, useful for creating unique temp files or tracking the script, as shown in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $# at step 3?
A2
B1
C0
D3
💡 Hint
Check the 'Value/Action' column for step 3 in the execution_table.
At which step does the script run a command and capture its exit status?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for the 'ls' command execution in the execution_table.
If the script was run without arguments, what would $1 and $# be?
A$1 is empty, $# is 1
B$1 is empty, $# is 0
C$1 is the script name, $# is 0
D$1 is the script name, $# is 1
💡 Hint
Refer to variable_tracker for how $1 and $# represent arguments.
Concept Snapshot
Special variables in bash:
$0 = script name
$1 = first argument
$# = number of arguments
$@ = all arguments
$? = last command exit status
$$ = current script PID
Use these to handle inputs and track script state.
Full Transcript
This lesson shows how bash special variables work. $0 holds the script name, $1 the first argument, $# the count of arguments, and $@ all arguments. The script runs a command 'ls' and $? captures its exit status. $$ shows the script's process ID. Watching these variables step-by-step helps understand how scripts get inputs and check command results.