โ† All Linux Flashcard Decks

Linux+ Basic Bash Scripting Flashcards

6 cards from real Linux practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 6 Linux+ Basic Bash Scripting flashcards as text
  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"`?

    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.

  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?

    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.

  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?

    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.

  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?

    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.

  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?

    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.

  6. What is the primary purpose of the `set -e` command at the beginning of a Bash script?

    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.