0
0
Bash Scriptingscripting~5 mins

Command-line arguments ($1, $2, ...) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does $1 represent in a bash script?
$1 is the first command-line argument passed to the script when you run it.
Click to reveal answer
beginner
How do you access the second command-line argument in a bash script?
You use $2 to get the second argument passed to the script.
Click to reveal answer
beginner
What does $# mean in a bash script?
$# gives the number of command-line arguments passed to the script.
Click to reveal answer
intermediate
How can you access all command-line arguments as a single string?
Use $* to get all arguments as one string separated by spaces.
Click to reveal answer
intermediate
What is the difference between $* and $@ in bash?
$* treats all arguments as a single word, while $@ treats each argument as a separate word, especially when quoted.
Click to reveal answer
In a bash script, what does $3 represent?
AThe third command-line argument
BThe total number of arguments
CThe script name
DThe first command-line argument
Which variable holds the number of command-line arguments in a bash script?
A$0
B$*
C$@
D$#
How do you access the script's own name inside a bash script?
A$#
B$1
C$0
D$@
What is the output of echo "$*" if the script is run with arguments: apple banana cherry?
Aapplebanana cherry
Bapple banana cherry
Capple,banana,cherry
Dapple
Which is true about "$@" in bash?
AIt treats each argument as a separate quoted string
BIt combines all arguments into one string
CIt counts the number of arguments
DIt holds the script name
Explain how to use command-line arguments in a bash script and how to access the first three arguments.
Think about how you run a script with extra words after the script name.
You got /5 concepts.
    Describe the difference between $* and $@ in bash scripting when handling multiple arguments.
    Consider how quoting affects the way arguments are grouped.
    You got /5 concepts.