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
What does `set -u` do in a bash script?
Answer: Treats unset variables as an error when they are expanded
`set -u` causes the script to exit with an error if an unset variable is referenced, catching typos early.
How do you append a line to a file inside a shell script without overwriting it?
Answer: echo 'line' >> file
`>>` appends to a file; `>` truncates and overwrites.
What is the output of `echo $((3 ** 2))` in bash?
Answer: 9
`**` is the exponentiation operator in bash arithmetic; `3 ** 2` equals 9.
Which syntax correctly declares an indexed array in bash?
Answer: arr=(one two three)
In bash, `arr=(value1 value2 ...)` initializes an indexed array with space-separated values.
What does `${arr[@]}` expand to when `arr` is a bash array?
Answer: All array elements as separate words
`${arr[@]}` expands each element as a separate word, preserving elements with spaces when quoted.
Which `getopts` call correctly processes a flag `-v` and an option `-o` that takes an argument?
Answer: getopts "vo:" opt
In `getopts`, a colon after a letter means it takes an argument; `vo:` means `-v` is a flag and `-o` requires a value.
What is the purpose of `exec > logfile.txt` inside a bash script?
Answer: Redirects all subsequent stdout of the script to logfile.txt
`exec > file` permanently redirects the shell's stdout to a file for all subsequent commands in that script.