Shell Scripting Questions with Answers

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

Sample questions

Question 1Easy

What does the first line #!/bin/bash do in a script?

  1. Marks the line as a comment only
  2. Imports the bash library
  3. Tells the system to run the script with bash
  4. Makes the script executable

Answer: C. Tells the system to run the script with bash

Explanation

The shebang names the interpreter. It still needs the execute bit to run as ./script.sh.

Step-by-step solution

  1. #! is the shebang.
  2. The kernel starts that interpreter.
  3. The script is passed as input.

Why the other options are wrong

  • A. It looks like a comment but has meaning.
  • B. There is no library import.
  • D. chmod +x is needed.

Question 2Easy

What must the first line of a shell script usually contain to choose the interpreter?

  1. import bash at the top
  2. #include <bash> at the top
  3. // bash
  4. A shebang such as #!/bin/bash

Answer: D. A shebang such as #!/bin/bash

Explanation

The shebang tells the kernel which interpreter runs the script.

Step-by-step solution

  1. The first line of a script is the shebang, which starts with #!.
  2. The path after #! names the interpreter, such as /bin/bash.
  3. So the answer is option D, a shebang such as #!/bin/bash.

Why the other options are wrong

  • A. Import is not a shell instruction; bash reads no such line.
  • B. #include is C preprocessor syntax, not a shell shebang.
  • C. // is a comment in C-style languages, not a shell interpreter line.

Tip: Shebang: #! followed by the interpreter path on line 1.

Question 3Easy

Inside a script, what does $0 hold?

  1. The first argument
  2. The number of arguments
  3. The name of the script as invoked
  4. The exit status

Answer: C. The name of the script as invoked

Explanation

$1 is the first argument, $# the count and $? the status.

Step-by-step solution

  1. Positional parameters start at $1.
  2. $0 is special.
  3. It is the command name.

Why the other options are wrong

  • A. That is $1.
  • B. That is $#.
  • D. That is $?.

Question 4Easy

Which test checks that a regular file named data.txt exists?

  1. [ -f data.txt ]
  2. [ -d data.txt ]
  3. [ -z data.txt ]
  4. [ -e data.txt = 1 ]

Answer: A. [ -f data.txt ]

Explanation

-f tests for a regular file. -d tests for a directory and -z for an empty string.

Step-by-step solution

  1. [ ] is the test command.
  2. -f means regular file.
  3. Exit status 0 means true.

Why the other options are wrong

  • B. -d is for directories.
  • C. -z tests string length zero.
  • D. Not valid syntax.

Question 5Medium

In bash, how do you read the first command-line argument in a script?

  1. $0
  2. $#
  3. $1
  4. $@[0]

Answer: C. $1

Explanation

$0 is the script name, $1 the first argument, $# the argument count.

Step-by-step solution

  1. First command-line argument is stored in $1.
  2. $1 represents the first positional parameter passed to the script.
  3. Correct answer is $1.

Why the other options are wrong

  • A. $0 is the script name, not the first argument.
  • B. $# is the count of arguments, not the value of the first one.
  • D. $@[0] is not valid bash for arguments; the first argument is $1.

Question 6Medium

What does ${name:-guest} evaluate to when name is unset or empty?

  1. An empty string
  2. guest
  3. name
  4. An error message

Answer: B. guest

Explanation

${var:-default} substitutes the default without assigning it.

Step-by-step solution

  1. name is unset.
  2. The :- form picks the default.
  3. name stays unset.

Why the other options are wrong

  • A. The default is used.
  • C. The text name is not used.
  • D. :? would raise an error.

Question 7Medium

What is the output?

x=5
y=$((x * 2 + 1))
echo $y
  1. 11
  2. 10
  3. 5*2+1
  4. 15

Answer: A. 11

Explanation

$(( )) performs integer arithmetic: 5 * 2 + 1 = 11.

Step-by-step solution

  1. X is assigned the value 5.
  2. Y is calculated as x * 2 + 1, which is 5 * 2 + 1 = 11.
  3. The echo command prints the value of y, which is 11.
  4. So the output is 11.

Why the other options are wrong

  • B. The +1 is added, so x * 2 + 1 gives 11, not 10.
  • C. Arithmetic expansion evaluates the expression, so the text 5*2+1 is not printed.
  • D. 15 would come from x * 3; the expression here is x * 2 + 1, which gives 11.

Tip: Remember operator precedence: * before + in arithmetic operations.

Question 8Medium

What is the output?

x=10
if [ $x -gt 9 ]; then echo yes; else echo no; fi
  1. no
  2. Error
  3. 10
  4. yes

Answer: D. yes

Explanation

-gt compares integers numerically: 10 > 9 is true.

Step-by-step solution

  1. $x is 10.
  2. 10 -gt 9 is true.
  3. The then branch runs.

Why the other options are wrong

  • A. The condition is true.
  • B. Syntax is valid.
  • C. It prints a word, not x.

Question 9Hard

Which is true about source script.sh versus ./script.sh?

  1. source runs in a new subshell
  2. source runs in the current shell, so variables it sets remain afterwards
  3. ./script.sh changes the current shell's variables
  4. They are the same

Answer: B. source runs in the current shell, so variables it sets remain afterwards

Explanation

A child process cannot change its parent's environment. source (or .) executes the commands inside the current shell.

Step-by-step solution

  1. ./script.sh starts a child.
  2. The child dies with its variables.
  3. source keeps them.

Why the other options are wrong

  • A. It's the opposite.
  • C. Children cannot change their parent.
  • D. They differ in process.

Question 10Hard

Which statement about trap 'rm -f /tmp/x' EXIT is correct?

  1. It stops the script from exiting
  2. It runs the cleanup command when the shell exits
  3. It removes /tmp/x immediately
  4. It traps only SIGINT

Answer: B. It runs the cleanup command when the shell exits

Explanation

trap installs handlers; the EXIT pseudo-signal fires when the shell ends.

Step-by-step solution

  1. The handler is registered.
  2. Nothing runs yet.
  3. At exit the command runs.

Why the other options are wrong

  • A. It does not block exit.
  • C. Not immediate.
  • D. EXIT is not SIGINT only.

Practise all 21 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