Top 31 Shell Scripting Interview Questions and Answers [Updated]

Top 31 Shell Scripting Interview Questions and Answers [Updated]

10 mins read2.7K Views Comment
Updated on Jun 2, 2023 16:02 IST

Shell scripting is a computer program to write 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.

2021_09_Shell-Scripting-Interview-Questions-scaled-e1631593024996.jpg

In this article, we have listed the most frequently asked shell scripting interview questions which will help you ace your next interview.

Top Shell Scripting Interview Questions and Answers

Here are the most frequently asked shell scripting interview questions covering the most important concepts of shell scripting.

Recommended online courses

Best-suited Linux courses for you

Learn Linux with these high-rated online courses

– / –
3 months
7 K
3 months
– / –
2 months
– / –
1 year
– / –
200 hours
– / –
3 months
– / –
3 months
– / –
2 months
– / –
3 months

Q1. What is a shell?

Ans. The shell acts as an interface between a user and the kernel. It provides users with an environment in which they can run commands, programs, and shell scripts. It is a command-line interpreter that translates commands provided by the user into kernel understandable language. The shell takes input from the users, communicates with the kernel to execute the program, and displays the output of the program.

Explore popular courses on Shiksha Online:

Popular Programming Courses Popular Operating System Courses
Top Cloud Technologies Courses Top Web Development Courses

Q2. What is a shell script?

Ans. A shell script is a series of UNIX commands to be executed, written in a plain text file. In shell scripting, a sequence of UNIX commands to be executed is provided instead of specifying just one command at a time. A shell script performs various functions such as program execution and file manipulation.

Q3. What are the advantages and advantages of shell scripting?

Ans. The advantages of shell scripting are:

  • Enables users to create their own series of commands
  • Runs a sequence of commands as a single command
  • Helps automate the repetitive tasks in the system administration
  • Saves time
  • Easy and quick

The disadvantages of shell scripting are:

  • Slow execution speed
  • Not suitable for complex tasks
  • Prone to errors

Check out free Linux Courses

Q4. What are shell variables? Which are the different types of variables used in shell scripting?

Ans. Shell variables are an important part of a shell script. The variables allow the shell to store and manipulate information within a shell program. A variable is a location in memory to which we assign a value. It is a character string to which we can assign a value in the form of number, text, filename, device, etc. A variable is a pointer to the actual data. A shell enables users to create, assign, or delete variables.

Below is the syntax to create a shell variable:

variable_name=variable_value

There are two types of variables in Shell Scripting:

  • System-defined variables – They are created by the operating system. These are defined in capital letters and can be viewed by using the “set” command.
  • User-defined variables – They are created by system users. A user can view the variable values using the “echo $variablename” command.

Explore popular Databases Courses

Q5. Name some of the commonly used UNIX commands.

Ans. Some of the commonly used UNIX commands are:

Command Description Syntax/Example
ls shows a list of files in the current directory $ ls
rm removes files from the system without confirmation rm filename
mv moves or renames a file Move: mv filename new_file_locationRename: mv filename newfilename
$ cat filename displays the contents present in a file $ cat filename
tar creates a new tar archive $ tar cvf archive_name.tar dirname/
grep searches for a given string in a file $ grep -i “the” test_file
finger displays information about user $ finger
passwd to change the password $ passwd
tty displays the device name of your terminal $ tty
date displays current date and time $ date
who displays the users who are logged in on the machine $ who
ps lists the processes currently running on the machine $ ps
gzip compresses the file $ gzip filename
history lists all the commands which are recently used $ history
lp displays name of the printer lp -dlpr -P
head $ head -15 file1 displays first 15 lines of the file
tail $ tail -15 file1 displays last 15 lines of the file
Difference between Linux and Unix
Learn Basic Linux Commands (With Syntax)
Top Unix Interview Questions and Answers for 2023

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

Ans. 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 )

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

Ans. 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.

Q8. What is a Superblock in shell scripting?

Ans. 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.

Q9. What are the different states of a Linux process?

Ans. The following are the four Linux process states:

  1. Waiting – Linux process waits for the resources.
  2. Running – Process is being executed.
  3. Stopped – Linux process stops after successful execution.
  4. Zombie – The process is no longer running but it remains active in the process table.

Explore the Top Online IT Courses

Q10. What is Crontab?

Ans. Crontab stands for Cron Table. It is a list of commands that a user wants to execute on a set schedule as well as the name of the command that manages that list. Crontab refers to the schedule and the name of the program used to edit the schedule. It automatically performs the tasks for the users as per the set schedule.

Crontab Syntax

 
[Minute] [hour]
[Day_of_the_Month]
[Month_of_the_Year]
[Day_of_the_Week]
Copy code

Q11. What is the role of while loop in shell scripting?

Ans. The while loop is used to repeatedly execute a group of statements while a command executes successfully. The syntax for the While loop in Shell scripting is:

 
while [ condition ]
do
command1
command2
..
.
commandN
done
Copy code

Q12. Why is C shell better than Bourne shell?

Ans. Below are the reasons why C Shell is better than Bourne Shell:

  • In the C shell, commands can be aliased. A user can give any name to a command.
  • Large and complex commands can be used again in the C shell.
  • C shell has a command history feature. Users can access all the previously used commands instead of typing the same command repeatedly.

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

Ans. Below is the shell script to generate the Fibonacci series:

 
#!/bin/bash
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="" <="" code=""></n))>
Copy code

Check out the Most In Demand Tech Skills

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

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

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

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

Q16. How can you pass arguments to a shell script?

Ans: Arguments can be passed to a shell script during execution time by including them after the script name. Inside the script, they can be accessed as $1, $2, etc., where $1 refers to first argument and $2 to second argument. This goes on similarly.

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

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

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

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

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

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

Q20. How to find all information of the user?

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

Top 65 Linux Interview Questions and Answers (2023)
What is Awk Command in Linux?
Understanding ps Command in Linux

Q21. Write the syntax for “nested if statement” in shell scripting?

Ans. The syntax for nested if statement is as follows:

 
if [ Condition ]
then
command1
command2
..
else
if [ condition ]
then
command1
command2
..
else
command1
command2
..
Copy code

Q22. Explain control instructions.

Ans. In shell scripting, control instructions represent how various instructions in a program are to be executed by the computer. They specify the flow of control in a shell program.

Q23. What is the role of the sed command?

Ans. sed is short for stream editor. It enables users to edit streams or files using regular expressions. It is used to perform different functions on a file, such as searching, finding and replacing, insertion or deletion. We can use the sed command to edit a file without using an editor. The editing done using the sed command is not permanent. It remains only on the display.

Syntax

 
sed options file
Copy code

Q24. What is the command to open a read-only file in Unix?

Ans. The command to open a read-only file in Unix is as follows:

 
vi –R
Copy code

Q25. Which command is used to create shortcuts in Linux?

Ans. We use the ‘link’ command to create shortcuts in Linux. Links are of two types: hard link and soft link.

Q26. Explain the different modes of vi editors.

Ans. The different modes of vi editors are:

  • Command mode: this is the mode when vi starts up.
  • Edit mode: enables users to do the next editing.
  • Ex mode: when users interact with vi with instructions to process.

Q27. Name the different types of control instructions in a shell.

Ans. There are four types of control instructions available in a shell:

  • Sequence Control Instruction
  • Decision Control Instruction
  • Loop Control Instruction
  • Case-Control Instruction

Q28. Write the Syntax for different loops in Shell Scripting.

Ans. The syntaxes for different loops in shell scripting are as follows:

For Loop Syntax:

 
for var in word1 word2 … wordn
do
Statement(s) to be executed for every word
done
Copy code

While Loop Syntax

 
while command
do
Statement(s) to be executed if the command is true
Done
Copy code

Until Loop Syntax

 
until command
do
Statement(s) to be executed if the command is true
done
Copy code

Q29. Name the two files of the crontab command.

Ans. The following are the two files of the crontab command. These files enable us to control access to the crontab command.

  • cron.allow – This file defines which users will be allowed to use the crontab command. Only the users who are listed in this file can create, modify, display, or remove crontab files.
  • cron.deny – This file specifies which will be prevented from using crontab command. The users that are listed in cron.deny file cannot use crontab.

Q30. How can you check the disk usage in Linux?

Ans. We can check the disk usage using the following commands:

  • df – df command is short for disk free. It displays the space taken up by different drives. It shows values in 1-kilobyte blocks.
  • df -h– We can add the -h option to display disk usage in a more human-readable form.
  • du – This command is used to display disk usage for individual directories.

Q31. Explain the difference between diff and cmp commands.

Ans. The diff command is short for ‘difference’. It represents the changes required to be made to make files identical. It displays the differences in the files by comparing the files line by line. On the other hand,  the cmp command compares two files byte by byte and shows the very first mismatch.

Conclusion

Armed with the in-depth knowledge of shell scripting, you can get numerous career advancement opportunities. We hope this blog on the most important shell script interview questions will help you gain a good understanding of the subject to excel in your next job interview.

FAQs

What is a shell?

The shell acts as an interface between a user and the kernel. It provides users with an environment in which they can run commands, programs, and shell scripts. It is a command-line interpreter that translates commands provided by the user into kernel understandable language. The shell takes input from the users, communicates with the kernel to execute the program, and displays the output of the program.

What is a shell script?

A shell script is a series of UNIX commands to be executed, written in a plain text file. In shell scripting, a sequence of UNIX commands to be executed is provided instead of specifying just one command at a time. A shell script performs various functions such as program execution and file manipulation.

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.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio