0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 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.