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
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.
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).
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.
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`.
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).
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.
Which construct runs `cmd2` only if `cmd1` succeeds?
Answer: cmd1 && cmd2
`&&` is the AND operator; `cmd2` executes only if `cmd1` exits with status 0.