If you are looking for a cloud computing job, you will need to prepare for a battery of questions that can help you excel at the job interview. Training Basket brings you list of top Bash Shell interview questions.
Q:1 What is Shell Script and why it is required ?
Ans: A Shell Script is a text file that contains one or more commands. As a system administrator we often need to issue number of commands to accomplish the task, we can add these all commands together in a text file (Shell Script) to complete daily routine task.
Q:2 What is the default login shell and how to change default login shell for a specific user ?
Ans: In Linux like Operating system “/bin/bash” is the default login shell which is assigned while user creation. We can change default shell using the “chsh” command . Example is shown below :
# chsh<username>-s <new_default_shell>
# chsh linuxtechi -s /bin/sh
Q:3 What are the different type of variables used in a shell Script ?
Ans: In a shell script we can use two types of variables :
- System defined variables
- User defined variables
System defined variables are defined or created by Operating System(Linux) itself. These variables are generally defined in Capital Letters and can be viewed by “set” command.
User defined variables are created or defined by system users and the values of variables can be viewed by using the command “echo $<Name_of_Variable>”
Q:4 How to redirect both standard output and standard error to the same location ?
Ans: There two method to redirect std output and std error to the same location:
Method:1 2>&1 (# ls /usr/share/doc > out.txt 2>&1 )
Method:2 &> (# ls /usr/share/doc &> out.txt )
Q:5 What is the Syntax of “nested if statement” in shell scripting ?
Basic Syntax is shown below :
if [ Condition ]
if [ condition ]
Q:6 What is the use of “$?” sign in shell script ?
Ans: While writing a shell script , if you want to check whether previous command is executed successfully or not , then we can use “$?” with if statement to check the exit status of previous command. Basic example is shown below :
If exit status is 0 , then command is executed successfully
[email protected]:~# ls /usr/bin/share
ls: cannot access /usr/bin/share: No such file or directory
[email protected]:~# echo $?
If the exit status is other than 0, then we can say command is not executed successfully.
Q:7 How to compare numbers in Linux shell Scripting ?
Ans: test command is used to compare numbers in if-then statement. Example is shown below :
if [ $x -gt $y ]
echo “x is greater than y”
echo “y is greater than x”
Q:8 What is the use of break command ?
Ans: The break command is a simple way to escape out of a loop in progress. We can use the break command to exit out from any loop, including while and until loops
Q:9 What is the use of continue command in shell scripting ?
Ans The continue command is identical to break command except it causes the present iteration of the loop to exit, instead of the entire loop. Continue command is useful in some scenarios where error has occurred but we still want to execute the next commands of the loop.
Q:10What is the basic syntax of while loop in shell scripting ?
Ans: Like the for loop, the while loop repeats its block of commands a number of times. Unlike the for loop, however, the while loop iterates until its while condition is no longer true. The basic syntax is :
while [ test_condition ]