1/ 🐚 Question: What is the purpose of the shebang (#!) at the beginning of a shell script? Give an example.
Answer: The shebang specifies the interpreter for the script.
Example: #!/bin/bash indicates the script is using the Bash shell.
Answer: The shebang specifies the interpreter for the script.
Example: #!/bin/bash indicates the script is using the Bash shell.
2/ 🐚 Question: How can you pass arguments to a shell script? Provide an example.
Answer: Use $1, $2, etc., for positional arguments.
Example: ./myscript.sh arg1 arg2.
Answer: Use $1, $2, etc., for positional arguments.
Example: ./myscript.sh arg1 arg2.
3/ 🐚 Question: Explain the difference between single and double quotes in shell scripting.
Answer: Single quotes preserve the literal value, while double quotes allow variable expansion.
Example: echo 'Hello $USER' vs. echo "Hello $USER".
Answer: Single quotes preserve the literal value, while double quotes allow variable expansion.
Example: echo 'Hello $USER' vs. echo "Hello $USER".
4/ 🐚 Question: What is command substitution, and how can you use it in a script? Provide an example.
Answer: Command substitution allows embedding command output.
Example: current_date=$(date).
Answer: Command substitution allows embedding command output.
Example: current_date=$(date).
5/ 🐚 Question: How do you check if a file exists in a shell script? Write a conditional example.
Answer: Use if [ -e "$filename" ]; then ... fi.
Answer: Use if [ -e "$filename" ]; then ... fi.
6/ 🐚 Question: Explain what the $? variable represents in shell scripting.
Answer: $? holds the exit status of the last command. 0 means success, non-zero indicates an error.
Answer: $? holds the exit status of the last command. 0 means success, non-zero indicates an error.
9/ 🐚 Question: What is process substitution in Bash? Give an example.
Answer: Process substitution allows treating the output of a command as a file.
Example: diff <(command1) <(command2).
Answer: Process substitution allows treating the output of a command as a file.
Example: diff <(command1) <(command2).
10/ 🐚 Question: How can you redirect both stdout and stderr to a file in a single command?
Answer: Use command &> output.log.
Answer: Use command &> output.log.
11/ 🐚 Question: Explain the purpose of the set -e option in a shell script.
Answer: set -e makes the script exit if any command returns a non-zero status.
Answer: set -e makes the script exit if any command returns a non-zero status.
12/ 🐚 Question: What is a pipeline in shell scripting, and why is it useful? Provide an example.
Answer: A pipeline connects the stdout of one command to the stdin of another.
Example: cat file.txt | grep "pattern".
Answer: A pipeline connects the stdout of one command to the stdin of another.
Example: cat file.txt | grep "pattern".
14/ 🐚 Question: What is process forking in shell scripting? Give an example.
Answer: Process forking is creating new processes.
Example: forked_process &.
Answer: Process forking is creating new processes.
Example: forked_process &.
15/ 🐚 Question: Explain the difference between == and = in a shell script's conditional statements.
Answer: == is used for string comparisons, while = is used for variable assignments.
Answer: == is used for string comparisons, while = is used for variable assignments.
18/ 🐚 Question: How can you check if a variable is empty in a shell script?
Answer: Use if [ -z "$var" ]; then ... fi.
Answer: Use if [ -z "$var" ]; then ... fi.
20/ 🐚 Question: Explain the difference between && and || in conditional statements.
Answer: && executes the right command if the left succeeds. || executes the right command if the left fails.
Answer: && executes the right command if the left succeeds. || executes the right command if the left fails.
1/ 🐚 Advanced Q8: How can you create a shell script that runs as a daemon or background process? 🚀
A1: Use '&' to run in the background and 'nohup' to detach from the terminal.
Example:
nohup ./myscript.sh &
A1: Use '&' to run in the background and 'nohup' to detach from the terminal.
Example:
nohup ./myscript.sh &
Repost the thread if you find it useful. Thanks!
Loading suggestions...