Shell scripting is a computer program to writes a sequence of commands for the shell to execute. It helps users learn the command line better and save time by automating commands and repetitive tasks. There is a huge demand for shell scripting professionals in India and across the world. Many leading organizations look for developers skilled in Unix shell scripting. In this article, we have listed the most frequently asked shell scripting interview questions which will help you ace your next interview.

1. What is Shell?

A Shell is basically a command-line interpreter between user and kernel or a complete environment specially designed to run commands, shell scripts, and programs. In this, whenever a user enters human-readable commands (input commands) through the keyboard, the shell communicates with the kernel to execute these commands, and display output in a shell script. Just as there are different flavors of operating systems, there are also different types of shells. Every shell has its own set of commands and functions. Shells issue the prompt, $, called a command prompt. You can type into the prompt while it is displayed. 

2. What is a Shell Script?

A shell script is a command-containing text file that contains commands in order of their execution. Typical operations performed by shell scripts include printing text, file manipulation, and program execution.

3. What are the advantages of Shell Scripting?

Following are the two main advantages of shell scripting:

  • It facilitates developing your own custom OS with relevant features that best suit your needs.
  • It facilitates designing software applications according to their respective platforms.
  • To automate the frequently performed operations
  • To run a sequence of commands as a single command
  • Easy to use
  • Portable (It can be executed in any Unix-like operating system without any modifications)

4. What are the disadvantages of Shell Scripting?

Following are the two main disadvantages of shell scripting:

  • Slow execution speed compared to any programming languages
  • A new process launched for almost every shell command executed

5. What is the importance of writing Shell Scripts?

Enlisted below points explain the importance of writing shell scripts.

  • Shell script takes input from the user, and file and displays it on the screen.
  • Shell scripting is very useful in creating your own commands.
  • It is helpful in automating some tasks of the day to day life.
  • It is useful for automating system administration tasks.
  • Mainly it saves time.

6. Shell programs are stored in which file?

Shell programs are stored in a file called sh.

7. What are the different types of Shells available?

There are mainly 4 important types of shells that are widely used. And they include:

  • Bourne Shell (sh)
  • C Shell (csh)
  • Korn Shell (ksh)
  • Bourne Again Shell (bash)
  • TENEX/TOPS C shell (TCSH)

8. Where we can use Shell Scripting?

Shell scripts can be used for the following applications: 

  • Automation of the code compiling process.
  • Run a program or create an environment for programming.
  • Complete batch processing and file manipulation.
  • Integrate existing programs.
  • Perform routine backups.
  • Keeping an eye on a system.
  • System administration’s tasks
  • Creating, maintaining, and implementing system boot script.

9. Why is a shell script needed?

Shell scripts can be written for a variety of reasons:

  • Keeping repetitive tasks to a minimum.
  • Can be used by system administrators for routine backups.
  • Monitoring the system.
  • Adding new functions to the shell.
  • Shell scripting allows you to create your own tools.
  • System admin can automate daily tasks.

10. What is the difference between Singe (‘) and Double (”) quotes?

Singe (‘) Quotes are used when the evaluation of variables to values is undesired. On the other hand, Double Quotes are used when the evaluation of variables to values is required.

11. What is a Superblock in shell scripting?

A Superblock is a collection of metadata to show the characteristics of filesystems in some types of operating systems. The superblock contains the records of properties including its size, the block size, the empty and the filled blocks and their respective counts, the size and location of the inode tables, the disk block map, and usage information.

12. What is the difference between = and ==?

= is used for assigning value to the variable while == is used for string comparison.

13. Where can you store the Shell programs in the system?

We can store the Shell programs in a file tagged as Sh (Bourne Shell) in the system.

14. Which command will you use to find out the number of arguments passed to the script?

echo $ # – this command will show the number of arguments passed to the script.

15. Which command is used to find the status of the process?

Ps ux – this command helps find the status of the process.

16. How will you get the end line from a file?

We will use tail -1 to get the end line from a file.

17. How to find all information of the user?

The finger the command will display all information of the users.

Practical Questions

18. How will you redirect the standard output and standard error to the same location?

We can redirect the standard output and standard error to the same location using the below methods:

&>(# ls /usr/share/doc &> out.txt )
2>&1(# ls /usr/share/doc > out.txt 2>&1 )

19. Write a shell script to generate the Fibonacci series.

!/bin/bash
##Formula of Fionacci series is: Fn = Fn-1 + Fn-2
c=2
a=1
b=1
d=0
echo "enter the number of elements: "
read n
echo "$a"
echo "$b"
while((c<n))
do
d=$((a+b))
echo "$d"
a=$b
b=$d
c=$((c+1))
done

Output:

enter the number of elements: 5
1
1
2
3
5

20. Write a shell script to add two numbers?

#!/bin/bash
#!/bin/bash
# Take input from user and calculate sum.
 
read -p "Enter first number: " num1
read -p "Enter second number: " num2
 
sum=$(( $num1 + $num2 ))
 
echo "Sum is: $sum" 

Output:

Enter first number: 12
Enter second number: 15
Sum is: 27

Leave a Reply