---
title: "Bash: Shell Scripting Fundamentals"
description: "Test your knowledge of Bash scripting fundamentals with a quiz covering variables, conditionals, loops, functions, I/O redirection, special parameters, and shell scripting best practices."
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/quizzes/post/bash-shell-scripting-fundamentals-quiz
---

# Bash: Shell Scripting Fundamentals

Welcome to the Bash Basics Quiz! This quiz covers fundamental Bash scripting concepts, syntax, and best practices. Each question is multiple-choice, and you'll find hints to help you along the way. Good luck!

## Questions

### 1. What is the purpose of the "shebang" line (#! /bin/bash) at the top of a script?

- It defines the character encoding for the text file
- It specifies the interpreter used to execute the script
- It grants the current user execution permissions
- It allocates a dedicated block of memory for the process

**Hint:** It tells the operating system how to read the file.

### 2. How do you correctly assign the value "Alpha" to a variable named "Project"?

- Project = Alpha
- Project="Alpha"
- $Project="Alpha"
- set Project "Alpha"

**Hint:** Bash is extremely sensitive to whitespace around the equals sign.

### 3. What does the variable "$?" represent?

- The Process ID of the currently running script
- The exit status of the most recently executed command
- The total number of arguments passed to the script
- The name of the script file currently being executed

**Hint:** Success is usually 0.

### 4. Which command is used to accept user input during a script?

- input
- read
- fetch
- accept

**Hint:** It "reads" from the standard input.

### 5. How do you check if a variable "VAR" is empty?

- [ -n "$VAR" ]
- [ -z "$VAR" ]
- [ -e "$VAR" ]
- [ -v "$VAR" ]

**Hint:** Think "Zero" length.

### 6. Which brackets are preferred in modern Bash for conditional tests?

- `( )`
- `[[ ]]`
- `{ }`
- `< >`

**Hint:** Double is more powerful than single.

### 7. How do you perform integer arithmetic to add 5 and 2?

- result = 5 + 2
- ((result = 5 + 2))
- result = $[5 + 2]
- let result 5 + 2

**Hint:** Use the double-parentheses syntax.

### 8. What does the "export" command do?

- Encodes the script into a portable binary format
- Makes a variable available to child processes
- Saves the current shell history to a local file
- Transmits the script to a remote network server

**Hint:** Think about parent and child processes.

### 9. Which loop syntax is correct for iterating through a list of files?

- `foreach file in *.txt`
- `for file in *.txt; do ... done`
- `loop file over *.txt { ... }`
- `while file in *.txt; do ... done`

**Hint:** For item in list...

### 10. What is the difference between single quotes (' ') and double quotes (" ")?

- Single quotes are for paths; double are for text
- Single quotes are literal; double quotes allow expansion
- Double quotes enable mathematical script operations
- Single quotes are required for all shell comments

**Hint:** One is literal, one allows expansion.

### 11. How do you capture the output of a command into a variable?

- `VAR = > command`
- `VAR=$(command)`
- `VAR = {command}`
- `VAR << command`

**Hint:** $(command)

### 12. What does the "break" keyword do in a loop?

- Interrupts the script and triggers an error
- Exits the loop and continues the script
- Restarts the current iteration from the top
- Suspends the script for a specified duration

**Hint:** Exiting the cycle.

### 13. Which special variable contains the number of arguments passed to a script?

- `$@`
- `$#`
- `$*`
- `$!`

**Hint:** Think "Count".

### 14. How do you define a function in Bash?

- `def my_func():`
- `my_func() { ... }`
- `function:my_func { ... }`
- `new func my_func { ... }`

**Hint:** function_name() { ... }

### 15. What is the "exit 1" command typically used for?

- To signal that the script completed successfully
- To signal that an error occurred during execution
- To return the script to the main menu function
- To pause the script until the user clicks a key

**Hint:** Ending with an error.

### 16. What does "2>&1" do in a command?

- It runs the command in a dual-core CPU mode
- It merges the error stream into the output stream
- It forces the command to run with root authority
- It hides all output from the terminal display

**Hint:** Redirecting streams.

### 17. Which comparison operator is used for strings in [[ ]], but NOT for integers?

- `-eq`
- `==`
- `-gt`
- `>`

**Hint:** The equals sign.

### 18. What is a "Here Document" (EOF)?

- A script header that contains author metadata
- A method for passing multi-line input to a command
- A specific file extension used for shell binaries
- A system signal sent when a file is corrupted

**Hint:** Feeding multiple lines of text into a command.

### 19. How do you run a script "backup.sh" in the background?

- `start backup.sh`
- `./backup.sh &`
- `run -bg backup.sh`
- `detach backup.sh`

**Hint:** Add a character at the end.

### 20. What does "set -x" do?

- Automatically terminates the script on any error
- Traces and displays each command before execution
- Sets the script to run in a read-only shell mode
- Enables extended regular expressions in the shell

**Hint:** Debugging.

### 21. Which variable represents all arguments passed to the script as individual quoted items?

- `$*`
- `$@`
- `$#`
- `$$`

**Hint:** The "At" symbol.

### 22. What is the result of echo ${#VAR}?

- It displays the current memory address of VAR
- It returns the character length of the variable
- It generates a cryptographic hash of the value
- It lists the number of times VAR was updated

**Hint:** The hash symbol before the name.

### 23. How do you check if a file is a directory?

- `[ -f "$FILE" ]`
- `[ -d "$FILE" ]`
- `[ -x "$FILE" ]`
- `[ -s "$FILE" ]`

**Hint:** Think "Directory".

### 24. What is the purpose of the "alias" command?

- To change the owner of a file or directory
- To create a custom shortcut for a command
- To hide a specific file from the directory list
- To synchronize a local file with a remote host

**Hint:** A nickname for a command.

### 25. Which command is used to replace text in a stream using regex?

- grep
- sed
- cat
- echo

**Hint:** Stream Editor.

### 26. How do you evaluate if $A is equal to $B for integers?

- `[ $A = $B ]`
- `[ $A -eq $B ]`
- `[ $A is $B ]`
- `[ $A == $B ]`

**Hint:** Integer Equality.

### 27. What happens if you run a command with a leading space (e.g., " command")?

- The command is executed with sudo privileges
- The command is excluded from the shell history
- The command runs in a separate background process
- The command is parsed as a shell comment

**Hint:** History management.

### 28. What is the result of "$(( 10 / 3 ))" in Bash?

- A decimal value of approximately 3.333
- A truncated integer value of 3
- A remainder value of 1 from the division
- A syntax error due to the lack of decimals

**Hint:** Bash math is limited.

### 29. Which command is used to make a script wait for a specific number of seconds?

- pause
- sleep
- wait
- delay

**Hint:** Think of "napping".

### 30. What is the purpose of "source script.sh"?

- To download the script from a remote server
- To run the script within the current shell
- To check the script for potential logic errors
- To convert the script into a binary executable

**Hint:** Running in the current shell.
