Sample questions
Question 1Easy
What does the first line #!/bin/bash do in a script?
- Marks the line as a comment only
- Imports the bash library
- Tells the system to run the script with bash
- 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
#! is the shebang.- The kernel starts that interpreter.
- 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?
- import bash at the top
- #include <bash> at the top
- // bash
- 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
- The first line of a script is the shebang, which starts with #!.
- The path after #! names the interpreter, such as /bin/bash.
- 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?
- The first argument
- The number of arguments
- The name of the script as invoked
- 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
- Positional parameters start at
$1. $0 is special.- 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?
- [ -f data.txt ]
- [ -d data.txt ]
- [ -z data.txt ]
- [ -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
[ ] is the test command.-f means regular file.- 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?
- $0
- $#
- $1
- $@[0]
Answer: C. $1
Explanation
$0 is the script name, $1 the first argument, $# the argument count.
Step-by-step solution
- First command-line argument is stored in $1.
- $1 represents the first positional parameter passed to the script.
- 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?
- An empty string
- guest
- name
- An error message
Answer: B. guest
Explanation
${var:-default} substitutes the default without assigning it.
Step-by-step solution
name is unset.- The
:- form picks the default. 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
- 11
- 10
- 5*2+1
- 15
Answer: A. 11
Explanation
$(( )) performs integer arithmetic: 5 * 2 + 1 = 11.
Step-by-step solution
- X is assigned the value 5.
- Y is calculated as x * 2 + 1, which is 5 * 2 + 1 = 11.
- The echo command prints the value of y, which is 11.
- 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
- no
- Error
- 10
- yes
Answer: D. yes
Explanation
-gt compares integers numerically: 10 > 9 is true.
Step-by-step solution
$x is 10.- 10 -gt 9 is true.
- 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?
source runs in a new subshellsource runs in the current shell, so variables it sets remain afterwards./script.sh changes the current shell's variables- 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
./script.sh starts a child.- The child dies with its variables.
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?
- It stops the script from exiting
- It runs the cleanup command when the shell exits
- It removes /tmp/x immediately
- 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
- The handler is registered.
- Nothing runs yet.
- 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 →