โ† All Linux Flashcard Decks

Shell Scripting Flashcards

7 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 7 Shell Scripting flashcards as text
  1. Which construct in bash properly iterates over all files in the current directory?

    Answer: for f in *; do echo "$f"; done

    Using glob `*` directly is safer than parsing `ls` output, which breaks on filenames with spaces.

  2. What does the special variable `$?` represent in a shell script?

    Answer: The exit status of the last command

    `$?` holds the exit code of the most recently executed foreground command.

  3. How do you redirect both stdout and stderr to the same file in bash?

    Answer: cmd > file 2>&1

    `2>&1` redirects stderr (fd 2) to wherever stdout (fd 1) currently points, combining both streams.

  4. Which command makes a shell script executable?

    Answer: chmod +x script.sh

    `chmod +x` adds the execute permission bit to the file.

  5. What is the purpose of `set -e` at the top of a bash script?

    Answer: Exit the script immediately on any command error

    `set -e` causes the script to exit immediately if any command returns a non-zero exit status.

  6. Which syntax correctly defines a function named `greet` in bash?

    Answer: greet() { echo hi; }

    In bash, `name() { commands; }` is the standard portable function definition syntax.

  7. What does `$(command)` do in a shell script?

    Answer: Captures the command's stdout as a string

    Command substitution `$(...)` replaces itself with the standard output of the enclosed command.