010-160 Linux Command Line & Shell Scripting 5 — Questions and Answers
Question 1: Which command substitution syntax stores the output of date into a variable TODAY?
- TODAY=[date]
- TODAY=(date)
- TODAY=$(date) (Correct answer)
- TODAY=<date>
Correct answer: TODAY=$(date)
$() performs command substitution, executing date and assigning its output to TODAY.
Question 2: What is the purpose of the shebang line #!/bin/bash at the top of a script?
- It is a comment and has no effect
- It specifies the interpreter that should execute the script (Correct answer)
- It sets the script's permissions automatically
- It imports the bash library
Correct answer: It specifies the interpreter that should execute the script
The shebang tells the OS which interpreter to use when the script is executed directly.
Question 3: Which command displays the first 5 lines of a file named data.csv?
- head -5 data.csv (Correct answer)
- top 5 data.csv
- first 5 data.csv
- cat -5 data.csv
Correct answer: head -5 data.csv
head -n N (or the shorthand head -N) prints the first N lines of a file.
Question 4: In bash, what does the double bracket [[ ... ]] provide compared to single bracket [ ... ]?
- Double brackets are slower and deprecated
- Double brackets support pattern matching and avoid word-splitting issues (Correct answer)
- Double brackets only work with numeric comparisons
- There is no functional difference between them
Correct answer: Double brackets support pattern matching and avoid word-splitting issues
[[ ]] is a bash keyword offering regex matching, no word-splitting, and safer quoting than POSIX [ ].
Question 5: Which command would you use to find all files modified in the last 24 hours under /home?
- ls -lt /home
- grep -mtime /home
- find /home -mtime -1 (Correct answer)
- locate -d 1 /home
Correct answer: find /home -mtime -1
find with -mtime -1 matches files whose modification time is less than 1 day (24 hours) ago.
Question 6: What does the cut -d: -f1 /etc/passwd command output?
- The encrypted passwords from /etc/passwd
- The first field (usernames) from each line of /etc/passwd (Correct answer)
- The last field (home directories) from /etc/passwd
- The number of lines in /etc/passwd
Correct answer: The first field (usernames) from each line of /etc/passwd
cut -d: sets colon as the delimiter, and -f1 extracts the first field, which is the username.
Question 7: Which bash conditional correctly checks if a file named config.txt exists?
- if exists config.txt; then
- if [ -f config.txt ]; then (Correct answer)
- if file config.txt; then
- if (config.txt); then
Correct answer: if [ -f config.txt ]; then
The -f test operator inside [ ] returns true if the specified path exists and is a regular file.
Which command substitution syntax stores the output of date into a variable TODAY?