Free Linux+ Basic Bash Scripting Questions and Answers 1 — Questions and Answers
Question 1: A Bash script named `process.sh` is executed with the following command: `./process.sh /var/log/syslog "systemd"` Inside the script, which special variable would hold the value `"systemd"`?
- $0
- $#
- $2 (Correct answer)
- $@
Correct answer: $2
In Bash scripting, positional parameters are used to access command-line arguments. `$0` contains the name of the script itself, `$1` contains the first argument, `$2` contains the second argument, and so on. `$#` holds the total number of arguments, and `$@` represents all arguments as separate strings.
Question 2: A system administrator wants to store the current date and time in a variable within a Bash script. Which of the following syntaxes correctly uses command substitution to achieve this?
- CURRENT_TIME='date'
- CURRENT_TIME={date}
- CURRENT_TIME=[date]
- CURRENT_TIME=$(date) (Correct answer)
Correct answer: CURRENT_TIME=$(date)
Command substitution allows the output of a command to replace the command itself. The modern and recommended syntax for command substitution is `$(command)`. The older syntax using backticks (`` `command` ``) also works but is less preferred due to nesting difficulties. The other options are incorrect syntax for this purpose.
Question 3: Examine the following Bash script snippet: ```bash #!/bin/bash COUNT=5 if [ $COUNT -gt 10 ]; then echo "Greater than 10" elif [ $COUNT -eq 5 ]; then echo "Equals 5" else echo "Something else" fi ``` What will be the output when this script is executed?
- Greater than 10
- Something else
- Equals 5 (Correct answer)
- No output will be produced.
Correct answer: Equals 5
The script first checks if the variable `COUNT` (which is 5) is greater than 10. This is false. It then proceeds to the `elif` (else if) condition, which checks if `COUNT` is equal to 5 (`-eq`). This is true, so the script executes the `echo` command within that block and prints "Equals 5". It then exits the conditional block.
Question 4: A script needs to check the exit status of the most recently executed command to determine if it was successful. Which special variable should be checked?
- $!
- $? (Correct answer)
- $$
- $#
Correct answer: $?
The special variable `$?` holds the exit status of the last command that was executed. A value of 0 indicates success, while any non-zero value indicates an error or failure. `$!` holds the process ID of the last background command, `$$` holds the process ID of the current shell, and `$#` holds the number of command-line arguments.
Question 5: Which of the following Bash loops is best suited for iterating through a known, finite list of items, such as a list of filenames?
- while
- until
- select
- for (Correct answer)
Correct answer: for
The `for` loop is specifically designed to iterate over a list of items (like filenames, a range of numbers, or elements in an array). A `while` loop continues as long as a condition is true, and an `until` loop continues as long as a condition is false; both are better for conditional looping rather than iterating over a static list. A `select` loop is used for creating interactive menus.
Question 6: What is the primary purpose of the `set -e` command at the beginning of a Bash script?
- To enable verbose mode, printing each command before it is executed.
- To treat unset variables as an error when performing parameter expansion.
- To exit the script immediately if any command fails (returns a non-zero exit status). (Correct answer)
- To disable filename generation (globbing).
Correct answer: To exit the script immediately if any command fails (returns a non-zero exit status).
The `set -e` option causes the script to exit immediately if a command exits with a non-zero status. This is a common practice for preventing scripts from continuing in an unpredictable state after a critical command has failed. `set -x` enables verbose mode, `set -u` treats unset variables as an error, and `set -f` disables globbing.
A Bash script named `process.sh` is executed with the following command:
`./process.sh /var/log/syslog "systemd"`
Inside the script, which special variable would hold the value `"systemd"`?