010-160 Linux Command Line & Shell Scripting 2 — Questions and Answers
Question 1: Which shell built-in command displays the value of a variable named MYVAR?
- print MYVAR
- echo $MYVAR (Correct answer)
- display MYVAR
- show $MYVAR
Correct answer: echo $MYVAR
The echo command followed by the variable name prefixed with $ displays its value.
Question 2: What does the pipe operator | do in a shell command?
- Redirects stdout to a file
- Sends the output of one command as input to another (Correct answer)
- Runs two commands simultaneously in background
- Appends output to an existing file
Correct answer: Sends the output of one command as input to another
The pipe | connects the stdout of the left command to the stdin of the right command.
Question 3: Which command lists only the directories inside /home?
- ls -f /home
- ls -d /home/*/ (Correct answer)
- ls -l /home | grep file
- ls -a /home
Correct answer: ls -d /home/*/
ls -d /home/*/ matches only directory entries by appending a trailing slash glob.
Question 4: In a bash script, what does the special variable $? represent?
- The script filename
- The number of arguments passed
- The exit status of the last command (Correct answer)
- The current process ID
Correct answer: The exit status of the last command
$? holds the exit code returned by the most recently executed foreground command.
Question 5: Which command would you use to search for the string 'error' in all .log files under /var/log?
- find /var/log -name error
- grep -r 'error' /var/log/*.log (Correct answer)
- locate error /var/log
- search 'error' /var/log
Correct answer: grep -r 'error' /var/log/*.log
grep with a pattern and a file glob searches for matching text within the specified files.
Question 6: What is the result of running: echo {a,b,c}.txt?
- Creates three files named a.txt b.txt c.txt
- Prints: a.txt b.txt c.txt (Correct answer)
- Produces a syntax error
- Prints: {a,b,c}.txt literally
Correct answer: Prints: a.txt b.txt c.txt
Brace expansion generates the strings a.txt, b.txt, and c.txt, which echo then prints.
Question 7: Which redirection operator appends standard output to an existing file without overwriting it?
- >
- >> (Correct answer)
- <
- 2>
Correct answer: >>
The >> operator opens the file in append mode, adding new output after existing content.
Which shell built-in command displays the value of a variable named MYVAR?