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
Which of the following is the correct extension for a shell script file? a) .txt b) .sh c) .script d) .bash
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
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
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
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
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
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
Which command would you use to stop a running shell script immediately? a) exit b) end c) stop d) cancel
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
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
What is the symbol used to indicate a comment in a shell script? a) // b) /* c) # d) --
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
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
How do you declare a variable in a shell script? a) declare var=value b) var = value c) set var = value d) variable value
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
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
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
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
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
Which operator is used for string comparison in shell scripts? a) == b) = c) ! d) !=
3. Comments and Documentation
Which of the following is the correct syntax for a comment in a shell script? a) /* comment */ b) # comment c) // comment d) -- comment
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
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
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
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
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
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
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
What is the command used to add comments to a shell script? a) # b) -- c) // d) /*
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
Qno
Answer
1
b) .sh
2
a) chmod +x filename.sh
3
a) #!/bin/bash
4
b) ./script.sh
5
b) To change the permissions of a script
6
a) cat filename.sh
7
c) By using bash -n script.sh
8
a) exit
9
a) By typing them after the script name
10
a) It indicates the script should be executed in the Bash shell
11
c) #
12
b) The script must start with a shebang (#!)
13
d) var=value
14
a) declare var=value
15
a) echo $var
16
a) Ends the script and optionally returns a status code
17
c) `var
18
d) All of the above
19
b) To return a status code from a function
20
a) ==
21
b) # comment
22
a) To explain and document the code for readability
23
d) The script will be harder to maintain and understand
24
d) Use multiple # for each line
25
c) Both A and B
26
b) It hides the comment from the output
27
a) By writing the script’s purpose at the beginning using #
28
c) Add comments at the beginning and end of the script
29
a) #
30
a) echo "Hello World" # This prints a greeting message