Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Special variables ($0, $1, $#, $@, $?, $) in Bash Scripting - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the name of the script being run.

Bash Scripting
echo "Script name is: [1]"
Drag options to blanks, or click blank then click option'
A$0
B$1
C$$
D$#
Attempts:
3 left
💡 Hint
Common Mistakes
Using $1 which is the first argument, not the script name.
Using $$ which is the process ID, not the script name.
2fill in blank
medium

Complete the code to print the number of arguments passed to the script.

Bash Scripting
echo "Number of arguments: [1]"
Drag options to blanks, or click blank then click option'
A$0
B$@
C$#
D$?
Attempts:
3 left
💡 Hint
Common Mistakes
Using $@ which lists all arguments, not the count.
Using $? which shows the last command's exit status.
3fill in blank
hard

Fix the error in the code to print all arguments as separate words.

Bash Scripting
echo [1]
Drag options to blanks, or click blank then click option'
A$#
B$0
C$?
D$@
Attempts:
3 left
💡 Hint
Common Mistakes
Using $# which gives only the number of arguments.
Using $0 which is the script name.
4fill in blank
hard

Fill both blanks to check if the last command succeeded and print its exit code.

Bash Scripting
if [ [1] -eq 0 ]; then
  echo "Success with exit code [2]"
fi
Drag options to blanks, or click blank then click option'
A$?
B$#
C$0
D$$
Attempts:
3 left
💡 Hint
Common Mistakes
Using $$ which is the process ID, not exit status.
Using $# which counts arguments, not exit status.
5fill in blank
hard

Fill all three blanks to print the script name, process ID, and all arguments.

Bash Scripting
echo "Script: [1]"
echo "PID: [2]"
echo "Args: [3]"
Drag options to blanks, or click blank then click option'
A$0
B$$
C$@
D$#
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up $@ and $# for arguments.
Using $? instead of $$ for process ID.

Practice

(1/5)
1. In a bash script, what does the special variable $# represent?
easy
A. The exit status of the last command
B. The name of the script
C. The number of arguments passed to the script
D. The process ID of the script

Solution

  1. Step 1: Understand the role of $#

    The variable $# in bash holds the count of all arguments passed to the script.
  2. Step 2: Differentiate from other special variables

    $0 is the script name, $$ is the process ID, and $? is the last command's exit status, so they don't represent argument count.
  3. Final Answer:

    The number of arguments passed to the script -> Option C
  4. Quick Check:

    $# = argument count [OK]
Hint: Remember: '#' means count, so $# counts arguments [OK]
Common Mistakes:
  • Confusing $# with $@ which holds all arguments
  • Thinking $0 is the argument count
  • Mixing up $? with argument count
2. Which of the following is the correct way to print the first argument passed to a bash script?
easy
A. echo $1
B. echo $0
C. echo $#
D. echo $$

Solution

  1. Step 1: Identify the variable for the first argument

    $1 holds the first argument passed to the script.
  2. Step 2: Check other options

    $0 is the script name, $# is the argument count, and $$ is the script's process ID, so they are incorrect for printing the first argument.
  3. Final Answer:

    echo $1 -> Option A
  4. Quick Check:

    First argument = $1 [OK]
Hint: Use $1 to access the first argument in bash scripts [OK]
Common Mistakes:
  • Using $0 instead of $1 for first argument
  • Confusing $# with argument value
  • Trying to print $$ for argument
3. Consider the script ./myscript.sh apple banana with this content:
echo "$0"
echo "$1"
echo "$#"
echo "$@"
echo "$$"

What will be the output of the third line echo "$#"?
medium
A. myscript.sh
B. 3
C. apple
D. 2

Solution

  1. Step 1: Count the arguments passed

    The script is called with two arguments: 'apple' and 'banana'. So, the count is 2.
  2. Step 2: Understand what $# prints

    $# prints the number of arguments, which is 2 here.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Arguments count = 2 [OK]
Hint: Count arguments after script name for $# [OK]
Common Mistakes:
  • Counting script name as argument
  • Confusing $@ with $#
  • Expecting argument values instead of count
4. You wrote this script snippet:
echo "Last command status: $?"
ls /nonexistent

But it always prints Last command status: 0 even if the directory doesn't exist. What is the problem?
medium
A. The ls command is correct, no problem
B. The echo runs before ls, so $? is from previous command
C. You should use $$ instead of $?
D. You need to quote $? differently

Solution

  1. Step 1: Understand when $? is evaluated

    $? holds the exit status of the last command executed before it.
  2. Step 2: Check command order

    The echo runs first, so it shows the status of the command before echo, not ls.
  3. Final Answer:

    The echo runs before ls, so $? is from previous command -> Option B
  4. Quick Check:

    Check command order for $? [OK]
Hint: Print $? immediately after the command to get correct status [OK]
Common Mistakes:
  • Using $$ instead of $? for exit status
  • Printing $? before running the command
  • Assuming $? holds script PID
5. You want to write a bash script that prints its own process ID and all arguments passed to it on separate lines. Which script snippet correctly does this?
hard
A. echo "PID: $$" echo "Args: $@"
B. echo "PID: $0" echo "Args: $#"
C. echo "PID: $?" echo "Args: $1"
D. echo "PID: $1" echo "Args: $$"

Solution

  1. Step 1: Identify variable for process ID

    $$ holds the current script's process ID.
  2. Step 2: Identify variable for all arguments

    $@ holds all arguments passed to the script as separate words.
  3. Final Answer:

    echo "PID: $$" echo "Args: $@" -> Option A
  4. Quick Check:

    PID = $$, all args = $@ [OK]
Hint: Use $$ for PID and $@ for all arguments [OK]
Common Mistakes:
  • Using $0 as PID instead of $$
  • Using $# (count) instead of $@ (all args)
  • Mixing $1 (first arg) with PID
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard