Shell Scripting Beginner Quiz
30 questions on Shell Scripting – Complete Beginner to Advanced Guide.
Question 1: What is a Shell in computing?
- A. The outer casing of the computer
- B. A command-line interpreter that provides an interface between the user and the operating system kernel — (correct answer)
- C. A type of database
- D. A web browser
Explanation: The shell accepts commands typed by the user, translates them, and passes them to the operating system's kernel to be executed.
Question 2: What is the difference between "Shell" and "Bash"?
- A. They are exactly the same
- B. Bash is the computer hardware, Shell is the software
- C. Shell is the general concept of a command interpreter, Bash (Bourne Again SHell) is one specific, highly popular implementation of a shell — (correct answer)
- D. Shell is for Windows, Bash is for Linux
Explanation: Other examples of shells include zsh, ksh, and sh. Bash is simply the default on most Linux systems.
Question 3: What does the echo command do in shell scripting?
- A. Repeats the last command executed
- B. Checks the network connection
- C. Prints the text provided to standard output (the screen) — (correct answer)
- D. Reboots the terminal
Explanation: echo "Server started" simply prints that message to the user in the terminal.
Question 4: How do you assign a value to a variable in a shell script?
- A.
set VAR to "Hello"
- B.
VAR="Hello" — (correct answer)
- C.
VAR = "Hello"
- D.
define VAR "Hello"
Explanation: In shell scripting, there must be NO spaces around the equals sign when assigning a variable.
Question 5: How do you use the value of the VAR variable you just created?
- A.
echo VAR
- B.
echo (VAR)
- C.
echo %VAR%
- D.
echo $VAR — (correct answer)
Explanation: You prefix the variable name with the $ sign when you want the shell to substitute it with its actual value.
Question 6: Which character tells the shell that the rest of the line is a comment?
- A.
//
- B.
/*
- C.
# — (correct answer)
- D.
--
Explanation: Comments are ignored by the shell and are used to explain what the script is doing to other developers.
Question 7: What does the > operator do?
- A. Compares two numbers
- B. Redirects the output of a command into a file, overwriting the file if it exists — (correct answer)
- C. Redirects the output to the printer
- D. Appends text to a file
Explanation: echo "Log" > file.txt completely replaces the contents of file.txt with "Log".
Question 8: What does the >> operator do?
- A. Fast forwards a process
- B. Redirects output into a file, overwriting it
- C. Appends the output of a command to the end of a file without deleting the existing content — (correct answer)
- D. Reads a file
Explanation: >> is essential for writing log scripts, as it adds new log entries to the bottom of the log file.
Question 9: What does the | (pipe) operator do?
- A. Pauses the script
- B. Takes the output of the command on the left and passes it as the input to the command on the right — (correct answer)
- C. Creates a visual table
- D. Exits the shell
Explanation: Pipes allow you to chain small, single-purpose commands together to perform complex tasks. Example: ls | grep "txt".
Question 10: How do you read user input and save it to a variable called CITY?
- A.
get CITY
- B.
input $CITY
- C.
read CITY — (correct answer)
- D.
ask CITY
Explanation: The read command pauses the script, waits for the user to type on the keyboard, and stores what they type in the CITY variable.
Question 11: In shell scripting, what does $0 represent?
- A. The first argument passed to the script
- B. The exit status of the script
- C. The name of the script currently being executed — (correct answer)
- D. The root directory
Explanation: If you run ./backup.sh, the value of $0 inside the script will be ./backup.sh.
Question 12: If you run a script like ./deploy.sh prod v1.0, what does $2 represent inside the script?
- A. deploy.sh
- B. prod
- C. v1.0 — (correct answer)
- D. Nothing
Explanation: Command-line arguments are stored in numbered variables. $1 is the first argument (prod), and $2 is the second (v1.0).
Question 13: What does the command chmod +x my_script.sh do?
- A. Compresses the script
- B. Deletes the script
- C. Grants "execute" permissions to the file, allowing it to be run as a program — (correct answer)
- D. Encrypts the script
Explanation: Without the +x (execute) permission, the system will deny permission when you try to run ./my_script.sh.
Question 14: Which operator checks if a file exists in a conditional statement?
- A.
-e — (correct answer)
- B.
-x
- C.
-z
- D.
-n
Explanation: if [ -e "config.txt" ] will evaluate to true if the file exists in the directory.
Question 15: Which operator checks if a string is empty (length is zero)?
- A.
-e
- B.
-z — (correct answer)
- C.
-empty
- D.
-0
Explanation: if [ -z "$VAR" ] is commonly used to check if the user forgot to provide a required command-line argument.
Question 16: What does $? store?
- A. The current process ID
- B. The exit status code of the most recently executed command — (correct answer)
- C. A random number
- D. The number of arguments passed
Explanation: Conventionally, an exit status of 0 means success, and anything greater than 0 (like 1 or 2) means an error occurred.
Question 17: Which keyword signifies the end of an if block in a shell script?
- A.
endif
- B.
done
- C.
end
- D.
fi — (correct answer)
Explanation: Shell scripting syntax closes the if block by spelling it backward: fi.
Question 18: What is the correct way to compare two integers in a shell script?
- A.
[ $A == $B ]
- B.
[ $A = $B ]
- C.
[ $A -eq $B ] — (correct answer)
- D.
[ $A === $B ]
Explanation: For integers, you use -eq (equal), -ne (not equal), -gt (greater than), -lt (less than). For strings, you use == or =.
Question 19: How do you capture the output of a command directly into a variable?
- A.
FILES="ls"
- B.
FILES=$(ls) — (correct answer)
- C.
FILES={ls}
- D.
FILES=run(ls)
Explanation: The $() syntax is called "Command Substitution". It executes the command inside the parentheses and replaces it with the output. (Older scripts use backticks ` ls `).
Question 20: What keyword is used to close a for loop or a while loop?
- A.
endloop
- B.
stop
- C.
done — (correct answer)
- D.
fi
Explanation: Loops start with do and finish with the done keyword.
Question 21: What is the correct syntax for an array in modern shell scripts (like Bash)?
- A.
MY_ARRAY={1, 2, 3}
- B.
MY_ARRAY=[1, 2, 3]
- C.
MY_ARRAY=(1 2 3) — (correct answer)
- D.
MY_ARRAY=1,2,3
Explanation: Arrays are defined using parentheses, and items are separated by spaces, not commas.
Question 22: How do you print the entirety of the array MY_ARRAY?
- A.
echo $MY_ARRAY
- B.
echo ${MY_ARRAY[@]} — (correct answer)
- C.
echo $MY_ARRAY.all
- D.
echo ${MY_ARRAY}
Explanation: Using echo $MY_ARRAY only prints the first element. The [@] syntax is required to expand all elements in the array.
Question 23: Which command is used to export a variable so it becomes available to other child processes or scripts called from within the current script?
- A.
share
- B.
global
- C.
export — (correct answer)
- D.
env
Explanation: export MY_VAR="value" makes the variable an Environment Variable, accessible to programs running inside that shell session.
Question 24: What does the set -x command do when placed at the top of a script?
- A. Enables strict execution mode
- B. Prints each command to the terminal right before it is executed, useful for debugging — (correct answer)
- C. Encrypts the code
- D. Hides all output
Explanation: set -x (xtrace) is the primary debugging tool for shell scripters to see exactly what the script is doing step-by-step.
Question 25: What does $# represent in a script?
- A. The script's process ID
- B. The exit code
- C. The total number of arguments passed to the script — (correct answer)
- D. The script's hash value
Explanation: If a user runs ./script.sh a b c, $# will evaluate to 3. This is used to verify the user provided enough arguments.
Question 26: What is the difference between single quotes (') and double quotes (")?
- A. Single quotes evaluate variables, double quotes do not
- B. Double quotes evaluate variables (e.g., "$VAR" becomes "Value"), single quotes treat everything as literal text (e.g., '$VAR' stays '$VAR') — (correct answer)
- C. They are completely identical
- D. Single quotes are used for numbers only
Explanation: Double quotes are "weak" quotes (allowing expansion), and single quotes are "strong" quotes (preventing any expansion).
Question 27: What does the && operator do?
- A. Runs both commands in the background
- B. Executes the second command only if the first command succeeds (exit code 0) — (correct answer)
- C. Adds two strings together
- D. Executes the second command only if the first command fails
Explanation: mkdir data && cd data ensures you don't attempt to cd into the directory if the mkdir command failed due to permissions.
Question 28: How do you suppress both the standard output and error messages of a command completely?
- A.
command > /dev/null 2>&1 — (correct answer)
- B.
command --silent
- C.
command | delete
- D.
command > /dev/zero
Explanation: This redirects stdout (1) to the /dev/null black hole, and redirects stderr (2) to wherever stdout is currently pointing (also the black hole).
Question 29: What is the syntax for a while loop that runs endlessly (infinite loop)?
- A.
while (true) { ... }
- B.
while true; do ... done — (correct answer)
- C.
loop forever; do ... done
- D.
while [ 1 ]; do ... end
Explanation: The true command does nothing but successfully return an exit code of 0. This makes the loop run indefinitely until a break is hit or the user presses Ctrl+C.
Question 30: What does the sleep command do?
- A. Shuts down the operating system
- B. Pauses the execution of the script for a specified number of seconds — (correct answer)
- C. Puts the hard drive to sleep
- D. Suspends a user account
Explanation: sleep 5 pauses the script for 5 seconds before moving to the next line. It is useful in while loops checking the status of an external service.