Week 01 Review — Interview Questions¶
50 questions covering shell fundamentals, files, text processing, variables, control flow, loops, I/O, and find.
The Shell & Terminal¶
1. What is the difference between a shell and a terminal emulator?
Show answer
A terminal emulator is the GUI application that provides a text window (GNOME Terminal, iTerm2, Windows Terminal). A shell is the program running inside it that interprets commands (bash, zsh, fish). The terminal handles display; the shell handles logic.
2. What does echo $0 show, and how can it differ from echo $SHELL?
Show answer
$0 is the name of the currently running shell process. $SHELL is your default login shell. If you are logged in with bash ($SHELL=/bin/bash) but you launched zsh manually, $0 shows zsh while $SHELL still shows bash.
3. What does type ls tell you that which ls does not?
Show answer
type ls shows whether ls is a builtin, alias, function, or external binary. which ls only searches $PATH for executables and cannot reveal aliases, functions, or builtins.
4. What does cd - do?
Show answer
It changes to the previous working directory ($OLDPWD). Useful for toggling between two directories without typing full paths.
Files & Permissions¶
5. What does chmod 755 mean?
Show answer
Owner (7 = rwx): read, write, execute. Group (5 = r-x): read and execute. Others (5 = r-x): read and execute. Standard for scripts and public directories.
6. What is the difference between chmod and chown?
Show answer
chmod changes permission bits (who can read/write/execute). chown changes ownership (which user and group own the file). Usually need sudo for chown.
7. Why does SSH refuse to use a private key with permissions 644?
Show answer
SSH requires private keys to be readable only by the owner (600 or stricter). If group or others can read the key, SSH considers it insecure and refuses to use it. Fix: chmod 600 ~/.ssh/id_rsa.
8. What is the sticky bit, and where is it used?
Show answer
On a directory, the sticky bit means only the file's owner (or root) can delete or rename files in it, even if others have write permission. Used on /tmp (drwxrwxrwt): everyone can create files, but only the owner can delete their own.
9. What is umask and how does it affect new files?
Show answer
umask defines which permission bits are removed from newly created files. Default 022: files get 644 (from 666 - 022), directories get 755 (from 777 - 022).
Text Processing¶
10. What is the difference between grep -c and grep | wc -l?
Show answer
grep -c counts matching lines per file and handles multiple files (showing file:count). grep | wc -l counts output lines, giving the same result for a single file but less cleanly for multiple files.
11. Why must you sort before using uniq?
Show answer
uniq removes only adjacent duplicate lines. Without sorting first, non-adjacent duplicates are not removed.
12. What does sort -n do differently from plain sort?
Show answer
Plain sort is lexicographic: 10 sorts before 9 (because 1 < 9 character-by-character). -n sorts numerically: 9 before 10.
13. What does awk '{print $NF}' print?
Show answer
$NF is the last field — NF is the number of fields, so $NF refers to the field numbered NF. This prints the last space-separated field on every line.
Stream Editing¶
14. What does sed 's/foo/bar/g' do?
Show answer
Replaces every occurrence of foo with bar on each line. Without g, only the first occurrence per line is replaced.
15. How do you delete blank lines from a file using sed?
Show answer
sed '/^$/d' file.txt — the pattern ^$ matches lines with nothing between start (^) and end ($). On macOS: sed -i '' '/^$/d' file.txt.
16. What is $0 in awk?
Show answer
The entire current line. $1, $2, etc. are individual fields. NF is the field count. NR is the record number.
Variables & Quoting¶
17. What happens when you reference a variable that was never assigned?
Show answer
By default, bash treats it as an empty string silently. With set -u, referencing an unset variable causes an error and exits the script.
18. What is the difference between "$@" and "$*"?
Show answer
"$@" expands to individually quoted words: "$1" "$2" "$3". "$*" expands to a single string: "$1 $2 $3". Use "$@" when passing arguments to preserve arguments containing spaces.
19. Why is count = 42 a syntax error?
Show answer
Bash sees count as a command with arguments = and 42. Assignment requires no spaces: count=42.
20. What does ${var:-default} do?
Show answer
Expands to default if var is unset or empty; otherwise expands to $var. Does not modify var. Use ${var:=default} to also set the variable.
Control Flow¶
21. What exit code means success in bash?
Show answer
0. Non-zero means failure. This is the opposite of most other languages. It makes sense because 0 = no error, and there are many possible error codes (1, 2, 127, etc.).
22. What is the difference between [](<#>) and [ ]?
Show answer
[](<#>) is a bash keyword: no word-splitting, supports &&/||, == with globs, =~ for regex, safer with empty variables. [ ] is the POSIX test command: more portable but has subtler quoting requirements.
23. How do you check if a file exists and is readable?
Show answer
[[ -f "$file" && -r "$file" ]] — -f checks it's a regular file, -r checks it's readable. Or [[ -r "$file" ]] alone (readable implies exists).
Loops¶
24. Why should you avoid for f in $(ls)?
Show answer
$(ls) is split on whitespace — filenames with spaces become multiple loop items. Use for f in * with "$f" quoted instead.
25. What is the safe pattern for reading a file line by line?
Show answer
while IFS= read -r line; do ...; done < file. IFS= prevents whitespace trimming; -r prevents backslash interpretation; < file redirect avoids subshell issues.
I/O & Pipes¶
26. What does 2>&1 mean?
Show answer
Redirect file descriptor 2 (stderr) to wherever file descriptor 1 (stdout) is currently pointing. The &1 means "the current target of fd 1." Order matters: cmd > file 2>&1 is correct; cmd 2>&1 > file is wrong (stderr goes to the terminal).
27. What does tee do?
Show answer
Reads from stdin, writes to stdout AND one or more files simultaneously. cmd | tee file.txt shows the output on the terminal and saves it to file.txt. Add -a to append instead of overwrite.
Find¶
28. What is the difference between -exec cmd {} \; and -exec cmd {} +?
Show answer
\; runs the command once per file. + passes all found files to a single command invocation (like xargs). + is significantly faster for large result sets.
29. Why use find -print0 | xargs -0 instead of a simple loop?
Show answer
Null-delimited transfer (-print0 / -0) handles filenames with spaces, newlines, and special characters correctly. A loop over $(find ...) breaks on filenames with spaces.
30. How do you find files modified in the last 24 hours?
Show answer
find /path -mtime -1 -type f — -mtime -1 means "modified less than 1 day ago." For minutes: -mmin -60 for the last 60 minutes.
Continue with week-02-review for Week 02 topics.