Sample questions
Question 1Easy
What does echo $((2 + 3 * 4)) print?
- 20
- 24
- 9
- 14
Answer: D. 14
Explanation
Arithmetic expansion follows the usual precedence: 3*4 = 12, plus 2 gives 14.
Step-by-step solution
- Multiply first: 12.
- Then add 2.
- Result 14.
Why the other options are wrong
- A. That is (2+3)*4.
- B. That is 2*3*4.
- C. That is 2+3+4.
Question 2Easy
Which command prints the value of the PATH environment variable?
- echo PATH
- echo $PATH
- print PATH
- show $PATH
Answer: B. echo $PATH
Explanation
$ expands a variable; echo PATH would print the literal word.
Step-by-step solution
- Echo $PATH uses $ to expand the PATH variable.
- $PATH becomes the actual path value before echo prints it.
- Echo PATH would print the literal string 'PATH' instead.
- Only option B correctly expands and prints the PATH value.
- Final answer: echo $PATH.
Why the other options are wrong
- A. Without $, echo prints the word PATH, not the value of the variable.
- C. print is not a bash command; echo is the command that prints text in bash.
- D. show is not a standard shell command; echo $PATH is how the variable is printed.
Tip: Use $variableName to print environment variables in shell commands.
Question 3Easy
What does export NAME=asha do compared with NAME=asha?
- Writes it to a file
- Makes it read-only
- Makes the variable available to child processes
- Makes it global for all users
Answer: C. Makes the variable available to child processes
Explanation
Unexported variables stay in the current shell; exported variables are copied into the environment of child processes.
Step-by-step solution
- Both set the variable.
export marks it for the environment.- Children inherit it.
Why the other options are wrong
- A. Nothing is saved.
- B. That is
readonly. - D. Scope is the process tree.
Question 4Easy
In bash, what does echo $((7 % 3)) print?
- 2
- 1
- 0
- 3
Answer: B. 1
Explanation
% is the remainder: 7 = 2*3 + 1.
Step-by-step solution
- The expression
7 % 3 calculates the remainder when 7 is divided by 3. - Dividing 7 by 3 gives a quotient of 2 and a remainder of 1.
- So,
echo $((7 % 3)) prints the remainder which is 1. - The correct answer is 1.
Why the other options are wrong
- A. 7 % 3 is the remainder 1; 2 is the quotient of 7 divided by 3, not the remainder.
- C. 0 would need an exact division, but 7 is not divisible by 3.
- D. 3 is not the remainder; 3 times 2 is 6, leaving 1 as the remainder.
Tip: Remember % gives the remainder, not the quotient.
Question 5Medium
What is the output?
X=1
(X=2)
echo $X
- 2
- 1
- Error
- Empty line
Answer: B. 1
Explanation
Parentheses run a subshell, so the change to X does not affect the parent shell.
Step-by-step solution
- X=1 sets X to 1 in the current shell.
- (X=2) runs in a subshell, so its change to X is lost when it ends.
- Echo $X prints 1.
Why the other options are wrong
- A. The subshell change to 2 does not affect the parent shell.
- C. No error happens; the line is valid bash.
- D. X is set to 1, so the line is not empty.
Tip: Parentheses ( ) run commands in a subshell; braces { } do not.
Question 6Medium
What does ls [0-9]*.log match?
- Files with a digit anywhere
- Files ending in a digit
- Log files whose name starts with a digit
- Files named exactly 0-9.log
Answer: C. Log files whose name starts with a digit
Explanation
The bracket expression [0-9] matches one digit, followed by anything, then .log.
Step-by-step solution
[0-9] is one digit.* follows..log ends the name.
Why the other options are wrong
- A. Digit is the first character.
- B. The digit is at the start.
- D. Brackets denote a range.
Question 7Medium
What is the output?
file=report.txt
echo ${file%.txt}
- txt
- report
- report.txt
- report.
Answer: B. report
Explanation
${var%pattern} removes the shortest matching suffix. ${var#pattern} removes a prefix.
Step-by-step solution
- Pattern is
.txt. - Suffix removed.
report remains.
Why the other options are wrong
- A. That is the extension.
- C. The suffix is removed.
- D. The dot is part of the pattern.
Question 8Medium
What is the output?
s=hello
echo ${#s}
- hello
- 1
- 5
- ${#s}
Answer: C. 5
Explanation
${#var} expands to the length of the string, which is 5.
Step-by-step solution
- The variable s is assigned the value 'hello'.
- ${#s} expands to the length of the string 'hello', which is 5.
- Echo prints the result of ${#s}, so it outputs 5.
- The correct answer is 5.
Why the other options are wrong
- A. ${s} would print hello, but ${#s} gives the length of the string, which is 5.
- B. The length of hello is 5 characters, not 1.
- D. ${#s} is valid bash and expands to 5 before echo prints it.
Tip: Remember: ${#var} returns the number of characters in the string.
Question 9Hard
A variable is set with Y=3 (no export). What does bash -c 'echo $Y' print?
- 3
- An empty line
- Y
- Error: unbound variable
Answer: B. An empty line
Explanation
Only exported variables are passed to child processes, so the child sees Y as empty.
Step-by-step solution
- Y=3 creates a shell variable that is not exported to child processes.
- Bash -c starts a new bash process, which does not inherit Y.
- So $Y expands to nothing and echo prints an empty line.
Why the other options are wrong
- A. The child process cannot see a non-exported variable.
- C. $Y expands to the value, not the name; here it expands to nothing.
- D. There is no error because bash does not use nounset (set -u) here.
Tip: Use export Y=3 so child processes can see the variable.
Question 10Hard
With f="my file.txt", why does rm $f behave badly?
- Variables cannot hold spaces
- The shell cannot expand f
- rm refuses dotted names
- Word splitting turns it into two arguments: my and file.txt
Answer: D. Word splitting turns it into two arguments: my and file.txt
Explanation
Unquoted expansions are split on whitespace. Quote them: rm "$f".
Step-by-step solution
$f expands to my file.txt.- The shell splits at the space.
- rm gets two arguments.
Why the other options are wrong
- A. They can.
- B. It expands fine.
- C. Dots are fine.
Tip: Always quote "$var".
Practise all 14 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →