MCQs on Introduction to Shell Scripts | Shell Scripting

Learn the essential concepts of shell scripting, including writing, executing scripts, understanding basic syntax and structure, using comments for documentation, and utilizing fundamental commands like echo and read.


MCQs on Introduction to Shell Scripts

1. Writing and Executing Scripts

  1. Which of the following is the correct extension for a shell script file?
    a) .txt
    b) .sh
    c) .script
    d) .bash
  2. How do you make a shell script executable in a Unix-based system?
    a) chmod +x filename.sh
    b) execute filename.sh
    c) chmod 755 filename.sh
    d) bash filename.sh
  3. What is the first line in a shell script that defines the shell to be used?
    a) #!/bin/bash
    b) #!/usr/bin/shell
    c) #!/sh
    d) #shell
  4. How can you execute a shell script named script.sh in the current directory?
    a) bash script.sh
    b) ./script.sh
    c) run script.sh
    d) execute script.sh
  5. What is the purpose of the chmod command in shell scripting?
    a) To create a script
    b) To change the permissions of a script
    c) To run a script
    d) To debug a script
  6. Which command is used to view the content of a shell script before executing it?
    a) cat filename.sh
    b) view filename.sh
    c) open filename.sh
    d) echo filename.sh
  7. If a script contains errors, how can you troubleshoot them?
    a) By using echo for debugging
    b) By running debug script.sh
    c) By using bash -n script.sh
    d) By adding #debug
  8. Which command would you use to stop a running shell script immediately?
    a) exit
    b) end
    c) stop
    d) cancel
  9. How do you pass arguments to a shell script when executing it?
    a) By typing them after the script name
    b) By modifying the script’s source code
    c) By using the args command
    d) By writing them in a config file
  10. What does the #!/bin/bash line in a script signify?
    a) It indicates the script should be executed in the Bash shell
    b) It runs the script in Python
    c) It specifies the script’s encoding format
    d) It sets environment variables

2. Basic Syntax and Structure

  1. What is the symbol used to indicate a comment in a shell script?
    a) //
    b) /*
    c) #
    d) --
  2. Which of the following is true about shell scripts’ structure?
    a) The script must begin with a function
    b) The script must start with a shebang (#!)
    c) Comments are not allowed
    d) The script must end with an exit command
  3. Which of the following statements is used to assign a value to a variable in shell scripting?
    a) var = value
    b) set var = value
    c) var := value
    d) var=value
  4. How do you declare a variable in a shell script?
    a) declare var=value
    b) var = value
    c) set var = value
    d) variable value
  5. How do you print the value of a variable in a shell script?
    a) echo $var
    b) print $var
    c) echo var
    d) show $var
  6. What does the exit command do in a shell script?
    a) Ends the script and optionally returns a status code
    b) Stops the current command
    c) Pauses the script
    d) Repeats the last command
  7. How can you assign a default value to a variable in shell scripting?
    a) var=default
    b) default var=value
    c) var ||= default
    d) var || default=value
  8. What type of statements are used to handle conditions in shell scripts?
    a) if, else, elif
    b) case
    c) while, for
    d) All of the above
  9. What is the purpose of the return command in shell scripting?
    a) To exit the script immediately
    b) To return a status code from a function
    c) To output a value
    d) To terminate a loop
  10. Which operator is used for string comparison in shell scripts?
    a) ==
    b) =
    c) !
    d) !=

3. Comments and Documentation

  1. Which of the following is the correct syntax for a comment in a shell script?
    a) /* comment */
    b) # comment
    c) // comment
    d) -- comment
  2. What is the purpose of using comments in shell scripts?
    a) To explain and document the code for readability
    b) To execute certain parts of the script
    c) To define variables
    d) To add inline comments for errors
  3. What happens if you forget to add a comment for a complex function in a shell script?
    a) It will cause a syntax error
    b) It will execute the code without any issues
    c) It will make the script run slower
    d) The script will be harder to maintain and understand
  4. How can you comment out a block of code in shell scripting?
    a) /* block of code */
    b) # block of code
    c) # block of code starts
    d) Use multiple # for each line
  5. Which of the following is true about comments in shell scripts?
    a) Comments can be placed at the beginning or end of a line
    b) Comments are ignored during script execution
    c) Both A and B
    d) Comments are mandatory
  6. What is the effect of using echo with comments?
    a) It displays the comment as output
    b) It hides the comment from the output
    c) It prints the comment at runtime
    d) It causes an error in the script
  7. How can you document the purpose of a script using a comment?
    a) By writing the script’s purpose at the beginning using #
    b) By using a special command
    c) By specifying a separate documentation file
    d) By using a documentation generator
  8. What is the best practice when commenting a shell script?
    a) Comment only when necessary
    b) Write comments for each line of code
    c) Add comments at the beginning and end of the script
    d) Comment all variables and functions
  9. What is the command used to add comments to a shell script?
    a) #
    b) --
    c) //
    d) /*
  10. Which of the following is an example of inline commenting?
    a) echo "Hello World" # This prints a greeting message
    b) echo # "Hello World"
    c) # echo "Hello World"
    d) echo "Hello World"; # Print greeting message

Answers

QnoAnswer
1b) .sh
2a) chmod +x filename.sh
3a) #!/bin/bash
4b) ./script.sh
5b) To change the permissions of a script
6a) cat filename.sh
7c) By using bash -n script.sh
8a) exit
9a) By typing them after the script name
10a) It indicates the script should be executed in the Bash shell
11c) #
12b) The script must start with a shebang (#!)
13d) var=value
14a) declare var=value
15a) echo $var
16a) Ends the script and optionally returns a status code
17c) `var
18d) All of the above
19b) To return a status code from a function
20a) ==
21b) # comment
22a) To explain and document the code for readability
23d) The script will be harder to maintain and understand
24d) Use multiple # for each line
25c) Both A and B
26b) It hides the comment from the output
27a) By writing the script’s purpose at the beginning using #
28c) Add comments at the beginning and end of the script
29a) #
30a) echo "Hello World" # This prints a greeting message

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