RHCSA RHCSA Shell Scripting and Automation 1 — Questions and Answers
Question 1: Which shebang line is used to indicate a Bash script on a RHEL system?
- #!/bin/bash (Correct answer)
- #!/bin/sh
- #!/usr/bin/bash
- #!/usr/bin/env sh
Correct answer: #!/bin/bash
The shebang `#!/bin/bash` explicitly invokes the Bash interpreter located at `/bin/bash` on RHEL systems.
Question 2: What command is used to make a shell script executable?
- chmod +x script.sh (Correct answer)
- chmod 644 script.sh
- chown +x script.sh
- exec script.sh
Correct answer: chmod +x script.sh
The `chmod +x` command adds the execute permission bit to a file, allowing it to be run as a script.
Question 3: In a Bash script, how do you store the output of the `date` command in a variable called `TODAY`?
- TODAY=$(date) (Correct answer)
- TODAY=date
- TODAY==date
- $TODAY=date
Correct answer: TODAY=$(date)
Command substitution using `$(command)` captures the standard output of a command and assigns it to a variable.
Question 4: Which loop structure is best suited for iterating over a list of filenames in a Bash script?
- for loop (Correct answer)
- while loop
- until loop
- select loop
Correct answer: for loop
The `for` loop is ideal for iterating over a finite list of items such as filenames or strings in Bash.
Question 5: What is the exit status of a Bash command that completes successfully?
- 0 (Correct answer)
- 1
- 255
- 127
Correct answer: 0
A successful command returns exit status `0`, while any non-zero value indicates an error or abnormal termination.
Question 6: How do you reference the first argument passed to a shell script?
- $1 (Correct answer)
- $0
- $@
- $#
Correct answer: $1
`$1` holds the value of the first positional parameter passed to the script on the command line.
Which shebang line is used to indicate a Bash script on a RHEL system?