Bash Scripting Beginner Quiz
30 questions on Bash Scripting – Complete Beginner to Advanced Guide.
Question 1: What is a Bash script?
- A. A compiled C program
- B. A text file containing a sequence of commands that the Bash shell executes — (correct answer)
- C. A database query language
- D. A graphical user interface tool
Explanation: Bash scripts automate repetitive tasks by writing terminal commands into a reusable text file.
Question 2: What is the very first line of a Bash script usually called?
- A. The Root Line
- B. The Header
- C. The Shebang — (correct answer)
- D. The Execution Tag
Explanation: The Shebang (#!) tells the operating system which interpreter to use to run the script.
Question 3: Which is the correct Shebang for a Bash script?
- A.
//bash
- B.
#!/bin/bash — (correct answer)
- C.
!#/bash/bin
- D.
<script type="bash">
Explanation: #!/bin/bash is the standard path to the Bash interpreter on most Linux and macOS systems.
Question 4: How do you make a Bash script file named script.sh executable?
- A.
run script.sh
- B.
make exe script.sh
- C.
chmod +x script.sh — (correct answer)
- D.
chown root script.sh
Explanation: chmod +x adds the "execute" permission to the file, allowing the system to run it as a program.
Question 5: How do you execute a script named script.sh in the current directory?
- A.
run script.sh
- B.
./script.sh — (correct answer)
- C.
start script.sh
- D.
execute script.sh
Explanation: The ./ tells the system to look in the exact current directory for the script, rather than searching the system's global PATH.
Question 6: Which command prints text to the screen in Bash?
- A. print
- B. write
- C. echo — (correct answer)
- D. show
Explanation: The echo command prints strings to standard output. Example: echo "Hello World".
Question 7: How do you assign the value "John" to a variable named NAME in Bash?
- A.
NAME = "John"
- B.
let NAME="John"
- C.
var NAME="John"
- D.
NAME="John" — (correct answer)
Explanation: In Bash, there must be NO spaces around the equals sign during variable assignment.
Question 8: How do you print the value of the variable NAME?
- A.
echo NAME
- B.
echo $NAME — (correct answer)
- C.
echo %NAME%
- D.
print(NAME)
Explanation: You must use the $ symbol to reference the value stored inside a variable.
Question 9: Which command is used to read input from the user during script execution?
- A. input
- B. prompt
- C. read — (correct answer)
- D. get
Explanation: The read command pauses the script and waits for the user to type something, storing it in a variable. Example: read USER_NAME.
Question 10: How do you add a comment in a Bash script?
- A.
// This is a comment
- B.
<!-- This is a comment -->
- C.
# This is a comment — (correct answer)
- D.
/* This is a comment */
Explanation: Everything following the # symbol on a line is ignored by the Bash interpreter (except for the Shebang on line 1).
Question 11: In a Bash script, what does the $1 variable represent?
- A. The total number of arguments
- B. The name of the script itself
- C. The first command-line argument passed to the script — (correct answer)
- D. The last command-line argument
Explanation: If you run ./script.sh apple banana, $1 is "apple" and $2 is "banana".
Question 12: What does the $? variable hold in Bash?
- A. The process ID of the script
- B. The exit status of the last executed command — (correct answer)
- C. A random number
- D. The current line number
Explanation: If a command succeeds, $? is 0. If it fails, $? is 1 (or another non-zero error code).
Question 13: What is the correct syntax for an if statement in Bash?
- A.
if [ condition ]; then ... fi — (correct answer)
- B.
if (condition) { ... }
- C.
if condition: ... end
- D.
if [ condition ] then ... endif
Explanation: Bash uses [ and ] for test conditions, requires then, and closes the block backwards with fi.
Question 14: Which flag is used in a test condition to check if a file exists?
- A.
-e
- B.
-x
- C.
-f
- D. Both A and C — (correct answer)
Explanation: -e checks if a file or directory exists. -f checks specifically if it is a regular file.
Question 15: How do you compare if two numbers are equal in a Bash [ test?
- A.
[ $a == $b ]
- B.
[ $a -eq $b ] — (correct answer)
- C.
[ $a = $b ]
- D.
[ $a === $b ]
Explanation: Bash uses -eq (equal), -ne (not equal), -gt (greater than), and -lt (less than) for integer comparisons.
Question 16: How do you compare if two strings are identical in Bash?
- A.
[ "$str1" -eq "$str2" ]
- B.
[ "$str1" == "$str2" ] — (correct answer)
- C.
[ "$str1" IS "$str2" ]
- D.
[ "$str1" === "$str2" ]
Explanation: While -eq is for numbers, = or == is used for string comparison inside the [ bracket.
Question 17: What does the && operator do between two commands?
- A. Runs both commands simultaneously
- B. Runs the second command ONLY if the first command succeeds — (correct answer)
- C. Runs the second command ONLY if the first command fails
- D. Adds the output of the two commands together
Explanation: && is a logical AND. Example: mkdir temp && cd temp will only change directories if the folder was successfully created.
Question 18: What does the || operator do between two commands?
- A. Runs the second command ONLY if the first command fails — (correct answer)
- B. Runs both commands in parallel
- C. Checks if two values are equal
- D. Redirects output
Explanation: || is a logical OR. Example: cd temp || echo "Folder not found!" prints the error only if cd fails.
Question 19: What is the correct syntax for a for loop that runs 5 times?
- A.
for i = 1 to 5; do ... done
- B.
for i in {1..5}; do ... done — (correct answer)
- C.
for (i=0; i<5; i++) { ... }
- D.
loop 5 times: ... end
Explanation: Bash uses sequence expansion {1..5} to easily loop over a range of numbers.
Question 20: How do you save the output of a command into a variable?
- A.
OUTPUT = run(ls)
- B.
OUTPUT=$(ls) — (correct answer)
- C.
OUTPUT={ls}
- D.
OUTPUT="ls"
Explanation: $(...) is known as command substitution. It runs the command inside and assigns its output to the variable.
Question 21: Which keyword closes a case statement block in Bash?
- A.
endcase
- B.
esac — (correct answer)
- C.
fi
- D.
break
Explanation: Just as if ends with fi, a case statement ends with case spelled backwards: esac.
Question 22: What is the correct way to define a function in Bash?
- A.
def my_func() { ... }
- B.
function my_func { ... }
- C.
my_func() { ... }
- D. Both B and C — (correct answer)
Explanation: You can define a function using the function keyword, or simply the name followed by ().
Question 23: Inside a Bash function, how do you access the first argument passed to that specific function?
- A.
$ARG1
- B.
$_1
- C.
$1 — (correct answer)
- D.
arg[0]
Explanation: Functions handle arguments exactly like scripts do. $1 is the first argument passed to the function.
Question 24: How do you define a variable that is only accessible inside a specific function?
- A.
private VAR="value"
- B.
local VAR="value" — (correct answer)
- C.
let VAR="value"
- D.
var VAR="value"
Explanation: Using the local keyword prevents the variable from overriding global variables with the same name.
Question 25: What does the command set -e do at the top of a script?
- A. Encrypts the script
- B. Exits the script immediately if any command returns a non-zero (error) status — (correct answer)
- C. Enables experimental features
- D. Echoes all commands to the terminal before running them
Explanation: set -e is a best practice. It prevents the script from blindly continuing to run and causing damage if a critical early step fails.
Question 26: What is the difference between single quotes (') and double quotes (") in Bash?
- A. There is no difference
- B. Single quotes allow variable expansion, double quotes do not
- C. Double quotes allow variable expansion (like
$NAME), single quotes treat everything as literal text — (correct answer)
- D. Single quotes are for characters, double quotes are for strings
Explanation: echo "$USER" prints "root". echo '$USER' literally prints "$USER".
Question 27: What does $@ represent in a script?
- A. The number of arguments
- B. All the command-line arguments passed to the script, treated as a list — (correct answer)
- C. The script's process ID
- D. The current directory
Explanation: $@ is highly useful when you want to loop through an unknown number of arguments provided by the user.
Question 28: How do you redirect both standard output (stdout) AND standard error (stderr) to a file named log.txt?
- A.
command > log.txt &> log.txt
- B.
command > log.txt 2>&1 — (correct answer)
- C.
command >> log.txt
- D.
command | log.txt
Explanation: > log.txt redirects stdout (file descriptor 1). 2>&1 tells stderr (file descriptor 2) to go to the exact same place as descriptor 1.
Question 29: What is the correct syntax for a while loop that reads a file line by line?
- A.
while read line; do ... done < file.txt — (correct answer)
- B.
while (file.txt) read line; do ... done
- C.
for line in file.txt; do ... done
- D.
read file.txt as line while; do ... done
Explanation: The < file.txt redirects the file into the while loop, and the read command processes it line by line.
Question 30: What does the /dev/null file represent in Linux scripting?
- A. A backup directory
- B. The trash bin for deleted files
- C. A "black hole" where data sent to it is discarded instantly and reading from it returns nothing — (correct answer)
- D. The root configuration file
Explanation: If a command produces annoying output you want to hide, you redirect it using > /dev/null 2>&1.