Shell scripting is essential for automating tasks and improving productivity. This set of 30 MCQs covers key shell scripting topics like variables, redirection, piping, and escaping to boost your expertise.
Shell Basics in Shell Scripting – MCQs
1. Variables and Environment Variables (10 Questions)
How do you define a variable in shell scripting? a) var name=value b) name=value c) set name=value d) define name=value
Which symbol is used to access the value of a variable in shell scripting? a) & b) $ c) # d) @
What is the purpose of environment variables in shell scripting? a) To store system-wide configuration values b) To store values only for the current script c) To perform arithmetic calculations d) To define shell functions
Which of the following is an example of an environment variable in Unix-based systems? a) $HOME b) username c) user123 d) my_var
How do you make a variable global in shell scripting? a) By using the global keyword b) By declaring the variable outside any functions c) By using the export command d) By assigning the variable to a specific environment
How can you check the value of an environment variable in shell scripting? a) echo $variable b) print variable c) show variable d) view variable
What is the default scope of variables in shell scripts? a) Global b) Local to the function c) Local to the script d) Permanent
How can you unset an environment variable in shell scripting? a) remove variable b) del variable c) unset variable d) erase variable
What will the command export MY_VAR="hello" do in a shell script? a) Define a local variable named MY_VAR b) Set a system-wide variable MY_VAR c) Export MY_VAR to the environment for child processes d) Display the value of MY_VAR
How do you assign a value to a variable in a shell script? a) set var=value b) var = value c) var=value d) value = var
2. Quoting and Escaping (10 Questions)
What is the purpose of using quotes in shell scripting? a) To define variable names b) To make special characters behave as literals c) To create environment variables d) To handle errors
Which quote allows for variable expansion in shell scripting? a) Single quotes (') b) Double quotes (") c) Backticks (`) d) No quotes
How can you escape a special character in shell scripting? a) Using a backslash (\) b) Using single quotes (') c) Using a backtick () d) Using a semicolon (;`)
What will the command echo "$VAR" do in a shell script if VAR="Hello"? a) Print $VAR literally b) Print Hello c) Print nothing d) Print an error
Which of the following will prevent a variable from expanding in the shell? a) Double quotes (") b) Backticks (`) c) Single quotes (') d) No quotes
What does the escape character \n represent in shell scripting? a) A space b) A newline c) A tab d) A comment
What is the correct way to handle a space in a file name within a shell script? a) Use quotes around the file name b) Replace spaces with underscores c) Escape spaces with a backslash (\) d) Remove spaces from the file name
What does the command echo 'I said "Hello"' do? a) Prints I said "Hello" b) Prints I said Hello c) Throws an error d) Prints I said \"Hello\"
What is the result of using single quotes (') around a variable in shell scripting? a) The variable will be expanded b) The variable will not be expanded c) The script will fail d) The variable value will be set to null
What does \t escape in shell scripting? a) A tab character b) A line break c) A space d) A carriage return
3. Input/Output Redirection (5 Questions)
What does the > symbol do in shell scripting? a) Appends output to a file b) Redirects output to a file c) Executes the previous command d) Shows the output of the command
How do you redirect standard error (stderr) to a file? a) command 2> error.log b) command > error.log c) command 1> error.log d) command >> error.log
What does the >> operator do in shell scripting? a) Redirects output to a file b) Appends output to a file c) Executes the command in the background d) Redirects both stdout and stderr
Which of the following is the correct command to redirect both standard output and standard error to a file? a) command &> file.txt b) command >> file.txt c) command 2>> file.txt d) command > file.txt
What does command < input.txt do in shell scripting? a) Redirects output to a file b) Redirects input from a file c) Executes a command with arguments from a file d) Shows the contents of a file
4. Piping Commands (5 Questions)
What is the purpose of the pipe | operator in shell scripting? a) To redirect input to a command b) To execute multiple commands sequentially c) To combine standard output from one command as input to another d) To display the output of a command
How do you display the contents of a file and count the number of lines in it using piping? a) cat file.txt | wc -l b) cat file.txt > wc -l c) wc -l file.txt d) cat file.txt & wc -l
Which command will display the contents of file.txt and pass it to grep for pattern searching? a) cat file.txt | grep "pattern" b) grep "pattern" file.txt c) cat file.txt > grep "pattern" d) grep "pattern" | cat file.txt
What will ps aux | grep "httpd" do in a shell script? a) Display all system processes related to httpd b) Start the httpd process c) Show memory usage for httpd d) Stop the httpd process
Which of the following will count the number of lines containing “error” in a log file? a) grep "error" log.txt | wc -l b) cat log.txt | wc -l c) grep "error" log.txt d) ps aux | wc -l
Answers
Qno
Answer
1
b) name=value
2
b) $
3
a) To store system-wide configuration values
4
a) $HOME
5
c) By using the export command
6
a) echo $variable
7
c) Local to the script
8
c) unset variable
9
c) Export MY_VAR to the environment for child processes
10
c) var=value
11
b) To make special characters behave as literals
12
b) Double quotes (")
13
a) Using a backslash ()
14
b) Print Hello
15
c) Single quotes (')
16
b) A newline
17
a) Use quotes around the file name
18
a) Prints I said "Hello"
19
b) The variable will not be expanded
20
a) A tab character
21
b) Redirects output to a file
22
a) command 2> error.log
23
b) Appends output to a file
24
a) command &> file.txt
25
b) Redirects input from a file
26
c) To combine standard output from one command as input to another