Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the exit code of the last command.
Bash Scripting
echo "Hello"; echo [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $$ which gives the process ID instead of the exit code.
Using $! which gives the PID of the last background command.
Using $# which gives the number of positional parameters.
✗ Incorrect
The special variable $? holds the exit code of the last executed command.
2fill in blank
mediumComplete the code to check if the last command succeeded (exit code 0).
Bash Scripting
some_command if [ [1] -eq 0 ]; then echo "Success"; fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $$ which is the process ID, not the exit code.
Using $! which is the PID of the last background command.
Using $# which is the number of arguments.
✗ Incorrect
The variable $? holds the exit code of the last command, which is 0 on success.
3fill in blank
hardFix the error in the code to correctly capture the exit code of a command.
Bash Scripting
ls /nonexistent_directory exit_code=[1] echo "Exit code: $exit_code"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $$ which is the process ID, not the exit code.
Using $! which is the PID of the last background command.
Using $# which is the number of arguments.
✗ Incorrect
The exit code of the last command is stored in $?. Assigning it to a variable captures it for later use.
4fill in blank
hardFill both blanks to create a script that runs a command and prints if it failed.
Bash Scripting
command_to_run if [ [1] -ne 0 ]; then echo "Command failed with exit code [2]" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for the exit code in condition and echo.
Using $$ or $# which are not exit codes.
✗ Incorrect
Use $? to check the exit code and also to print it. Both blanks need the exit code variable.
5fill in blank
hardFill all three blanks to save the exit code, check it, and print a message.
Bash Scripting
some_command exit_code=[1] if [ [2] -eq 0 ]; then echo "Command succeeded with code [3]" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $? directly in the if after assigning it to a variable.
Using $$ which is the process ID.
✗ Incorrect
Assign $? to exit_code, then check exit_code and print it. This saves and reuses the exit code.