0
0
Bash Scriptingscripting~5 mins

Special variables ($0, $1, $#, $@, $?, $) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the special variable $0 represent in a bash script?
$0 holds the name of the script or command being executed. It helps you know which script is running.
Click to reveal answer
beginner
What information does $1 provide in a bash script?
$1 is the first argument passed to the script. If you run ./script.sh hello, then $1 is hello.
Click to reveal answer
beginner
Explain the purpose of $# in bash scripting.
$# tells you how many arguments were passed to the script. It helps check if the user gave the right number of inputs.
Click to reveal answer
intermediate
What does $@ represent in a bash script?
$@ is all the arguments passed to the script, treated as separate words. Useful to loop over all inputs.
Click to reveal answer
intermediate
What is the use of $? and $$ in bash?
$? shows the exit status of the last command (0 means success). $$ is the process ID of the current shell, useful for unique temp files.
Click to reveal answer
In a bash script, what does $# represent?
AThe number of arguments passed to the script
BThe name of the script
CThe first argument
DThe process ID of the script
Which special variable holds the exit status of the last command?
A$0
B$?
C$@
D$$
If you want to access all arguments passed to a bash script as separate words, which variable do you use?
A$1
B$0
C$#
D$@
What does $$ represent in a bash script?
AThe process ID of the current script
BThe script name
CThe first argument
DThe number of arguments
What value does $0 hold inside a bash script?
AThe last argument
BThe number of arguments
CThe script's filename or command name
DThe exit status of last command
Describe the purpose of each special variable: $0, $1, $#, $@, $?, and $$ in bash scripting.
Think about what each variable tells you about the script or its inputs.
You got /6 concepts.
    How would you use special variables to check if a user passed exactly two arguments to your bash script?
    Counting arguments helps control script behavior.
    You got /3 concepts.