MCQs on Shell Basics | Shell Scripting

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)

  1. How do you define a variable in shell scripting?
    a) var name=value
    b) name=value
    c) set name=value
    d) define name=value
  2. Which symbol is used to access the value of a variable in shell scripting?
    a) &
    b) $
    c) #
    d) @
  3. 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
  4. Which of the following is an example of an environment variable in Unix-based systems?
    a) $HOME
    b) username
    c) user123
    d) my_var
  5. 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
  6. 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
  7. What is the default scope of variables in shell scripts?
    a) Global
    b) Local to the function
    c) Local to the script
    d) Permanent
  8. How can you unset an environment variable in shell scripting?
    a) remove variable
    b) del variable
    c) unset variable
    d) erase variable
  9. 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
  10. 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)

  1. 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
  2. Which quote allows for variable expansion in shell scripting?
    a) Single quotes (')
    b) Double quotes (")
    c) Backticks (`)
    d) No quotes
  3. 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 (;`)
  4. 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
  5. Which of the following will prevent a variable from expanding in the shell?
    a) Double quotes (")
    b) Backticks (`)
    c) Single quotes (')
    d) No quotes
  6. What does the escape character \n represent in shell scripting?
    a) A space
    b) A newline
    c) A tab
    d) A comment
  7. 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
  8. 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\"
  9. 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
  10. 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)

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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)

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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

QnoAnswer
1b) name=value
2b) $
3a) To store system-wide configuration values
4a) $HOME
5c) By using the export command
6a) echo $variable
7c) Local to the script
8c) unset variable
9c) Export MY_VAR to the environment for child processes
10c) var=value
11b) To make special characters behave as literals
12b) Double quotes (")
13a) Using a backslash ()
14b) Print Hello
15c) Single quotes (')
16b) A newline
17a) Use quotes around the file name
18a) Prints I said "Hello"
19b) The variable will not be expanded
20a) A tab character
21b) Redirects output to a file
22a) command 2> error.log
23b) Appends output to a file
24a) command &> file.txt
25b) Redirects input from a file
26c) To combine standard output from one command as input to another
27a) cat file.txt
28a) cat file.txt
29a) Display all system processes related to httpd
30a) grep “error” log.txt

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top