20 Linux Scripting Quiz Questions and Answers

Linux scripting refers to the practice of writing scripts to automate tasks, manage system resources, and perform operations on a Linux-based operating system. At its core, it involves using command-line interpreters like Bash, which is the most common shell, to execute sequences of commands stored in a file. These scripts can handle file management, process control, text processing, and network operations, making them essential for system administrators and developers.

Key elements of Linux scripting include:
Variables and Data Handling: Scripts use variables to store data, enabling dynamic operations like string manipulation and arithmetic calculations.
Control Structures: Conditional statements (if-else), loops (for, while), and case statements allow for decision-making and repetition in scripts.
Functions: Reusable blocks of code that modularize scripts, improving readability and maintainability.
Input/Output Redirection: Directing command output to files or other commands, which is crucial for logging and data processing.
Error Handling: Mechanisms like exit statuses and traps to manage and respond to errors gracefully.

Linux scripting is versatile for tasks such as automating backups, scheduling jobs with cron, parsing logs, or building custom tools. It requires a solid understanding of Linux commands and best practices for security, such as avoiding hard-coded sensitive information. With tools like sed, awk, and grep, scripts become powerful for text processing and data analysis, enhancing productivity in environments like servers, embedded systems, and cloud computing.

Table of contents

Part 1: OnlineExamMaker AI quiz generator – Save time and efforts

Still spend a lot of time in editing questions for your next Linux scripting assessment? OnlineExamMaker is an AI quiz maker that leverages artificial intelligence to help users create quizzes, tests, and assessments quickly and efficiently. You can start by inputting a topic or specific details into the OnlineExamMaker AI Question Generator, and the AI will generate a set of questions almost instantly. It also offers the option to include answer explanations, which can be short or detailed, helping learners understand their mistakes.

What you may like:
● Automatic grading and insightful reports. Real-time results and interactive feedback for quiz-takers.
● The exams are automatically graded with the results instantly, so that teachers can save time and effort in grading.
● LockDown Browser to restrict browser activity during quizzes to prevent students searching answers on search engines or other software.
● OnlineExamMaker API offers private access for developers to extract your exam data back into your system automatically.

Automatically generate questions using AI

Generate questions for any topic
100% free forever

Part 2: 20 Linux scripting quiz questions & answers

  or  

1. Question: What command is used to display text on the standard output in a Bash script?
Options:
A. print
B. echo
C. display
D. show
Answer: B
Explanation: The echo command is specifically designed to output text or variables to the console in Bash scripts.

2. Question: How do you declare a variable in a Bash script?
Options:
A. var = value
B. $var = value
C. var=value
D. set var value
Answer: C
Explanation: In Bash, variables are assigned using the syntax var=value without spaces around the equal sign.

3. Question: What is the purpose of the shebang line in a Bash script?
Options:
A. To define variables
B. To specify the interpreter, like !/bin/bash
C. To add comments
D. To execute loops
Answer: B
Explanation: The shebang line, such as !/bin/bash, indicates the path to the interpreter that should run the script.

4. Question: Which loop is used to iterate over a list of items in Bash?
Options:
A. for loop
B. while loop
C. until loop
D. do-while loop
Answer: A
Explanation: The for loop in Bash is ideal for iterating over a sequence, such as files or numbers, using syntax like for i in list; do … done.

5. Question: What does the command `chmod +x script.sh` do?
Options:
A. Deletes the script
B. Makes the script executable
C. Edits the script
D. Copies the script
Answer: B
Explanation: The chmod +x command adds execute permissions to the file, allowing it to be run as a script.

6. Question: How do you check if a file exists in a Bash script?
Options:
A. if [ -e file ]; then
B. if [ exists file ]; then
C. if file exists; then
D. if [ -f file ]; then
Answer: A
Explanation: The -e test in an if statement checks if the file exists, regardless of its type.

7. Question: What is the output of `echo $((2 + 3))` in Bash?
Options:
A. 2 + 3
B. 5
C. Error
D. $(2 + 3)
Answer: B
Explanation: The $(( )) syntax performs arithmetic expansion, so it evaluates and outputs the result of 2 + 3 as 5.

8. Question: Which command is used to read user input in a Bash script?
Options:
A. input
B. read
C. getinput
D. echo
Answer: B
Explanation: The read command allows a script to accept input from the user and store it in a variable.

9. Question: What does the `exit 0` command do in a Bash script?
Options:
A. Terminates the script with a success status
B. Starts a new process
C. Loops indefinitely
D. Deletes files
Answer: A
Explanation: The exit command with a status of 0 indicates that the script completed successfully.

10. Question: How do you create a function in Bash?
Options:
A. function name() { … }
B. def name() { … }
C. fn name() { … }
D. create name() { … }
Answer: A
Explanation: Bash functions are defined using the syntax function name() { commands; } or simply name() { commands; }.

11. Question: What is the effect of `set -e` in a Bash script?
Options:
A. Enables debugging
B. Exits the script if any command fails
C. Sets variables
D. Starts a loop
Answer: B
Explanation: The set -e option makes the script exit immediately if any command returns a non-zero status, helping with error handling.

12. Question: Which symbol is used for comments in Bash scripts?
Options:
A. //
B. C. —
D. /*Answer: B
Explanation: The symbol denotes a comment in Bash, and anything following it on the line is ignored by the interpreter.

13. Question: How do you append output to a file in Bash?
Options:
A. echo “text” > file.txt
B. echo “text” >> file.txt
C. echo “text” | file.txt
D. echo “text” + file.txt
Answer: B
Explanation: The >> operator appends the output to the end of the file, whereas > overwrites it.

14. Question: What does the `grep` command do in a script?
Options:
A. Searches for patterns in files
B. Deletes files
C. Copies files
D. Edits text
Answer: A
Explanation: Grep is used to search and filter text using regular expressions, making it essential for pattern matching.

15. Question: In Bash, how do you use an if-else statement?
Options:
A. if condition; then … else … fi
B. if condition { … } else { … }
C. if condition; else … end
D. if condition then … else … end
Answer: A
Explanation: The standard if-else syntax in Bash is if [condition]; then commands; else commands; fi.

16. Question: What is the purpose of the `source` command?
Options:
A. To run a script in the current shell
B. To create a new shell
C. To delete a file
D. To list files
Answer: A
Explanation: The source command executes a script in the current shell environment, applying changes like variable settings immediately.

17. Question: How do you handle command-line arguments in Bash?
Options:
A. Using $1, $2, etc.
B. Using args[1], args[2]
C. Using input$1
D. Using var1, var2
Answer: A
Explanation: Positional parameters like $1, $2 represent the first, second argument, and so on, passed to the script.

18. Question: What does `&&` do in a Bash command?
Options:
A. Runs the next command only if the previous one succeeds
B. Adds numbers
C. Creates a loop
D. Pipes output
Answer: A
Explanation: The && operator executes the following command only if the preceding command returns a zero exit status.

19. Question: How do you redirect both stdout and stderr in Bash?
Options:
A. command > file 2>&1
B. command > file
C. command 2> file
D. command | file
Answer: A
Explanation: Using > file redirects stdout, and 2>&1 redirects stderr to the same place as stdout.

20. Question: What is the difference between single quotes and double quotes in Bash strings?
Options:
A. Single quotes prevent variable expansion, double quotes allow it
B. Double quotes prevent expansion, single quotes allow it
C. Both are the same
D. Single quotes are for numbers only
Answer: A
Explanation: In double quotes, variables like $var are expanded, while in single quotes, they are treated as literal text.

  or  

Part 3: AI Question Generator – Automatically create questions for your next assessment

Automatically generate questions using AI

Generate questions for any topic
100% free forever