Globbing & Variables Questions with Answers

14 practice questions on Globbing & Variables (Linux & Shell) with answers and step-by-step solutions. Try 10 samples below, then practise all 14 free in the AptiNest app.

Sample questions

Question 1Easy

What does echo $((2 + 3 * 4)) print?

  1. 20
  2. 24
  3. 9
  4. 14

Answer: D. 14

Explanation

Arithmetic expansion follows the usual precedence: 3*4 = 12, plus 2 gives 14.

Step-by-step solution

  1. Multiply first: 12.
  2. Then add 2.
  3. 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?

  1. echo PATH
  2. echo $PATH
  3. print PATH
  4. show $PATH

Answer: B. echo $PATH

Explanation

$ expands a variable; echo PATH would print the literal word.

Step-by-step solution

  1. Echo $PATH uses $ to expand the PATH variable.
  2. $PATH becomes the actual path value before echo prints it.
  3. Echo PATH would print the literal string 'PATH' instead.
  4. Only option B correctly expands and prints the PATH value.
  5. 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?

  1. Writes it to a file
  2. Makes it read-only
  3. Makes the variable available to child processes
  4. 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

  1. Both set the variable.
  2. export marks it for the environment.
  3. 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?

  1. 2
  2. 1
  3. 0
  4. 3

Answer: B. 1

Explanation

% is the remainder: 7 = 2*3 + 1.

Step-by-step solution

  1. The expression 7 % 3 calculates the remainder when 7 is divided by 3.
  2. Dividing 7 by 3 gives a quotient of 2 and a remainder of 1.
  3. So, echo $((7 % 3)) prints the remainder which is 1.
  4. 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
  1. 2
  2. 1
  3. Error
  4. 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

  1. X=1 sets X to 1 in the current shell.
  2. (X=2) runs in a subshell, so its change to X is lost when it ends.
  3. 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?

  1. Files with a digit anywhere
  2. Files ending in a digit
  3. Log files whose name starts with a digit
  4. 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

  1. [0-9] is one digit.
  2. * follows.
  3. .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}
  1. txt
  2. report
  3. report.txt
  4. report.

Answer: B. report

Explanation

${var%pattern} removes the shortest matching suffix. ${var#pattern} removes a prefix.

Step-by-step solution

  1. Pattern is .txt.
  2. Suffix removed.
  3. 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}
  1. hello
  2. 1
  3. 5
  4. ${#s}

Answer: C. 5

Explanation

${#var} expands to the length of the string, which is 5.

Step-by-step solution

  1. The variable s is assigned the value 'hello'.
  2. ${#s} expands to the length of the string 'hello', which is 5.
  3. Echo prints the result of ${#s}, so it outputs 5.
  4. 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?

  1. 3
  2. An empty line
  3. Y
  4. 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

  1. Y=3 creates a shell variable that is not exported to child processes.
  2. Bash -c starts a new bash process, which does not inherit Y.
  3. 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?

  1. Variables cannot hold spaces
  2. The shell cannot expand f
  3. rm refuses dotted names
  4. 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

  1. $f expands to my file.txt.
  2. The shell splits at the space.
  3. 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 →

Related topics

See all Linux & Shell topics · All topics