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
In bash, which operator tests whether a string is non-empty?
Answer: [ -n "$var" ]
`-n` returns true if the string length is nonzero; `-z` tests for empty string.
What will `echo ${#myvar}` print if `myvar="hello"`?
Answer: 5
`${#varname}` expands to the length (number of characters) of the variable's value.
Which line should appear at the very top of a bash script to specify the interpreter?
Answer: #!/bin/bash
The shebang `#!/bin/bash` tells the kernel which interpreter to use when the script is executed directly.
How do you pass the value of variable `name` to a script as its first argument and access it inside?
Answer: ./script.sh "$name"; access as $1
Arguments are passed on the command line and accessed inside the script as positional parameters `$1`, `$2`, etc.
What is the effect of `trap 'rm -f /tmp/tmpfile' EXIT` in a script?
Answer: Runs the cleanup command whenever the script exits for any reason
`trap CMD EXIT` registers a command to run automatically whenever the script exits, enabling reliable cleanup.
Which bash feature allows `case "$var" in pattern) ... esac` to match multiple patterns?
Answer: Separate them with |
In a `case` statement, the `|` character separates alternative patterns for the same block.
What does `read -r line` do differently from `read line` in bash?
Answer: Prevents backslash from being treated as an escape character
The `-r` flag disables backslash interpretation, so backslashes are read literally — essential for reading file paths.