Shell Scripting Interview Questions

 Shell Scripting Interview Questions


Shell Scripting Interview Questions

Shell Scripting Interview Questions



1.What is a shell script?

Answer: A shell script is a text file containing a series of commands that are executed by the shell (command-line interpreter) of an operating system. It allows for automation of tasks and the execution of multiple commands in sequence.

 

2.What is the difference between a shell and a shell script?

Answer: A shell is a command-line interpreter that processes command and executes them. A shell script, on the other hand, is a file containing a set of commands that can be executed by the shell.

 

3.How do you define a shebang in a shell script?

Answer: A shebang (#!) is placed at the beginning of a shell script to specify the path of the shell that should be used to interpret the script. For example, #!/bin/bash specifies that the script should be executed using the Bash shell.

 

4.How do you pass command-line arguments to a shell script?

Answer: Command-line arguments can be passed to a shell script using variables called positional parameters. These variables are accessed as $1, $2, $3, and so on, representing the first, second, third argument, respectively.

 

5.How do you define and use variables in a shell script?

Answer: Variables in a shell script are defined by assigning a value to them, for example, myVar="Hello". They can be used by referencing the variable name preceded by a dollar sign, for example, echo $myVar.

 

6.What is command substitution in shell scripting?

Answer: Command substitution allows the output of a command to be substituted as part of another command or assigned to a variable. It is denoted by using backticks (command) or the $(command) syntax.

 

7.How do you redirect standard output and standard error in shell scripting?

Answer: Standard output (stdout) and standard error (stderr) can be redirected using the > and >> operators. For example, command > file.txt redirects stdout to a file, and command 2> error.txt redirects stderr to a file.

 

8.How do you read user input in a shell script?

Answer: User input can be read using the read command. For example, read myVar will prompt the user for input and store it in the variable myVar.

 

9.How do you use conditional statements in shell scripting?

Answer: Conditional statements can be used in shell scripting using the if-else-fi syntax. For example:

if [condition]; then

  # code to be executed if the condition is true

else

  # code to be executed if the condition is false

fi

 

10.How do you use loops in shell scripting?

Answer: Loops can be used in shell scripting using the for and while loops. For example:

for item in list; do

  # code to be executed for each item in the list

done

 

while [condition]; do

  # code to be executed as long as the condition is true

done

 

11.What are exit codes in shell scripting?

Answer: Exit codes are numeric values returned by a command or a script to indicate the success or failure of the execution. An exit code of 0 represents success, while non-zero values indicate different types of errors or failures.

 

12.How do you handle errors in shell scripting?

Answer: Errors can be handled in shell scripting by checking the exit codes of commands using the $? variable. Additionally, you can use conditional statements and error handling techniques like trapping signals and using the set -e option.

 

13.What are functions in shell scripting?

Answer: Functions in shell scripting allow you to group a set of commands together and execute them as a single unit. They help modularize code and improve reusability.

 

14.How do you pass arguments to a shell script function?

Answer: Arguments can be passed to a shell script function in the same way as command-line arguments. They are accessed using the positional parameters ($1, $2, $3, and so on) within the function.

 

15.What are environment variables in shell scripting?

Answer: Environment variables are predefined variables that hold information about the system environment. They can be accessed and modified by shell scripts and are used to configure and control the behaviour of programs.

 

16.How do you handle signals in shell scripting?

Answer: Signals can be handled in shell scripting using the trap command. It allows you to specify actions to be taken when a signal is received, such as capturing and handling interrupts (SIGINT) or clean-up actions on script termination (SIGTERM).

 

17.How do you perform arithmetic operations in shell scripting?

Answer: Arithmetic operations can be performed in shell scripting using the expr command, double parentheses (( )), or the $(( )) syntax. For example, result=$((num1 + num2)) calculates the sum of num1 and num2 and stores it in the variable result.

 

18.How do you use arrays in shell scripting?

Answer: Arrays can be used in shell scripting by declaring them and accessing their elements. For example:

myArray=("apple" "banana" "orange")

echo ${myArray[0]} # prints the first element of the array

 

19.How do you run a shell script in the background?

Answer: A shell script can be run in the background by appending an ampersand (&) at the end of the command. For example, ./script.sh & will run the script in the background.

 

20.How do you comment code in a shell script?

Answer: Comments in shell scripts are denoted by using the hash (#) symbol. Anything after the hash symbol on the same line is considered a comment and is ignored by the shell.






https://www.youtube.com/@it-academy1/


https://www.youtube.com/@it-academy1/









Previous
Next Post »