โ† 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 bash arithmetic syntax correctly increments a variable `count`?

    Answer: ((count++))

    `(( ))` is bash's arithmetic evaluation context, where C-style expressions like `count++` are valid.

  2. What does the `-f` flag test for in `[ -f filename ]`?

    Answer: File exists and is a regular file

    `-f` checks that the path exists and is a regular file (not a directory or device).

  3. How does `while IFS= read -r line; do ...; done < file.txt` work?

    Answer: Reads file.txt line by line, preserving whitespace and backslashes

    Setting `IFS=` prevents whitespace stripping and `-r` prevents backslash processing, giving raw lines.

  4. What is the result of `echo "${var:-default}"` when `var` is unset?

    Answer: Prints 'default'

    `${var:-default}` expands to `default` if `var` is unset or empty, without modifying `var`.

  5. Which command within a script will print the script's own filename?

    Answer: echo $0

    `$0` holds the name or path of the running script (or the shell name if running interactively).

  6. What happens when you use `local varname` inside a bash function?

    Answer: Restricts the variable's scope to that function

    `local` limits a variable's visibility to the function (and its children), preventing pollution of the global scope.

  7. Which construct runs `cmd2` only if `cmd1` succeeds?

    Answer: cmd1 && cmd2

    `&&` is the AND operator; `cmd2` executes only if `cmd1` exits with status 0.