Shell and Command-line Programming - Multiple Choice Questions
Questions
1. What does the shebang line `#!/bin/bash` indicate in a shell script?
a) A comment in the script
b) The start of a function
c) Specifies the interpreter to be used
d) Defines a system variable
2. Which command is used to make a script executable?
a) execute [Link]
b) chmod +x [Link]
c) run [Link]
d) permit [Link]
3. What does the `$@` variable represent in a shell script?
a) The first command-line argument
b) The total number of arguments
c) All command-line arguments as a list
d) The script's name
4. How do you declare an array in Bash?
a) array = (1 2 3)
b) array := [1, 2, 3]
c) array = {1, 2, 3}
d) fruits=(apple banana cherry)
5. What is the purpose of the `read` command?
a) Read a file
b) Read system information
c) Read user input
d) Read directory contents
6. Which symbol is used for output redirection to append to a file?
a) >
b) >>
c) >>>
d) +>
7. What does the `-z` test operator check in a conditional statement?
a) If a number is zero
b) If a string is empty
c) If a file exists
d) If a directory is zero-sized
8. How do you define a function in Bash?
a) function name() { }
b) def name():
c) create name() { }
d) make name() { }
9. What does `set -e` do in a shell script?
a) Sets environment variables
b) Enables debugging
c) Exits the script on any error
d) Increases script verbosity
10. Which command is used to count lines in a file?
a) count lines
b) wc -l
c) lines file
d) number file
11. What is the purpose of the `trap` command?
a) To create a network trap
b) To handle signals and perform cleanup
c) To monitor system processes
d) To set file permissions
12. How do you access the second argument in a shell script?
a) $1
b) $2
c) $0
d) $#
13. What does the `getopts` command help with?
a) Getting system options
b) Parsing command-line options
c) Retrieving file options
d) Checking system settings
14. Which comparison operator checks if two strings are equal?
a) ==
b) ===
c) =
d) eq
15. What is the purpose of `IFS` in shell scripting?
a) Internal File Separator
b) Input File System
c) Intermediate Function Selector
d) Internal Function Separator
16. How do you print the length of a string?
a) len(string)
b) [Link]()
c) ${#string}
d) length string
17. What does the `2>&1` redirection do?
a) Redirects standard error to standard output
b) Creates a new file descriptor
c) Closes all file descriptors
d) Redirects output to error log
18. Which command is used to evaluate arithmetic expressions?
a) math
b) calc
c) expr
d) evaluate
19. What is the purpose of the `local` keyword in a function?
a) To define a global variable
b) To create a local scope for a variable
c) To lock a variable
d) To make a variable read-only
20. How do you check if a file exists in a conditional statement?
a) file exists
b) -e file
c) exists file
d) [Link]()
Answers with Explanations
1. c) Specifies the interpreter to be used
2. b) chmod +x [Link]
3. c) All command-line arguments as a list
4. d) fruits=(apple banana cherry)
5. c) Read user input
6. b) >>
7. b) If a string is empty
8. a) function name() { }
9. c) Exits the script on any error
10. b) wc -l
11. b) To handle signals and perform cleanup
12. b) $2
13. b) Parsing command-line options
14. a) ==
15. a) Internal Field Separator
16. c) ${#string}
17. a) Redirects standard error to standard output
18. c) expr
19. b) To create a local scope for a variable
20. b) -e file