Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Shell Scripting?
3.
Most Asked Shell Scripting Interview Questions
3.1.
1. What are a shell and a shell script?
3.2.
2. Give some advantages and disadvantages of shell script?
3.3.
3. Define shell variables and explain the types of variables used in shell scripting.
3.4.
4. In which file are shell programs stored?
3.5.
5. What are the different sorts of shells widely used on a Linux system?
3.6.
6. Define superblock in shell scripting.
3.7.
7. What are features of shell script?
3.8.
8. Explain in brief the four stages of the Linux process.
3.9.
9. What benefits does C Shell have over Bourne Shell?
3.10.
10.  Explain the difference between Bourne Shell and C Shell.
3.11.
11. Is it necessary to use a different compiler to run a shell program?
3.12.
12.  How many shell scripts are included with the UNIX operating system?
3.13.
13. When should you avoid using shell programming or scripting?
3.14.
14. What are the limitations of shell scripting?
3.15.
15. Define GUI scripting?
3.16.
16. Define Crontab.
3.17.
17. In shell scripting, what is the purpose of the while loop?
3.18.
18. What is the difference between the commands, break and continue?
3.19.
19.  In Shell Scripting, what is the importance of the Shebang line?
3.20.
20. Distinguish between $@ and $*.
3.21.
21. How long does a shell variable last?
3.22.
22. Tell us how to use a shell script to compare strings.
3.23.
23. Is it possible to distinguish between hard and soft connections in shell scripting? 
3.24.
24. How will you troubleshoot issues with the shell program?
3.25.
25. What is the state of a process started with the shell's exec command?
3.26.
26. Explain how to use the controls in shell scripting.
3.27.
27.  What are control instructions?
3.28.
28. How many types of control instructions are available in a shell?
3.29.
29. What are metacharacters?
3.30.
30. Explain ways to create shortcuts in Linux.
3.31.
31. What are the default permissions of a file when it is created?
3.32.
32. What are the methods for passing and accessing arguments to a script in Linux?
3.33.
33. What is meant by “s” permission bit in a file?
3.34.
34. What steps would you take to transfer a file from one machine to another?
3.35.
35. What steps would you take to establish a connection to a database server from a Linux environment?
3.36.
36. Explain the process of configuring an array in Linux.
4.
Frequently Asked Questions
4.1.
Is shell scripting a skill?
4.2.
How do I prepare myself for the technical interview round?
4.3.
How can I impress my technical interviewers?
4.4.
Is shell scripting difficult to understand?
4.5.
How much time is required to learn shell scripting?
5.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Top Shell Scripting Interview Questions in 2023

Author Ishita mishra
0 upvote

Introduction

Have you ever wondered what those crucial questions are deciding factors for your selection in the interview round? If your answer is yes, then get ready, ninjas, as we have curated a list of some of the most important shell-scripting interview questions that you can study to ace your interview.

Shell Scripting Interview Questions

In this article, we will go through some essential shell-scripting interview questions on shell scripting and their answers often asked in shell-scripting interviews.

So without further ado, let's dive head first into Shell scripting and some crucial shell scripting interview questions.

What is Shell Scripting?

A shell script is a text file containing a set of commands for UNIX-based operating systems. It's termed a shell script because it condenses a series of commands that would otherwise have to be typed one at a time on the keyboard into a single script.

Shell scripting offers automation, facilitates the completion of monotonous tasks, and facilitates system monitoring. It is simpler to begin using it. Shell scripting is used by system administrators for numerous purposes, including routine backups. If you have prior programming skills, learning will be relatively simple (a very basic level will also work fine).

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Most Asked Shell Scripting Interview Questions

Here is a list of the top shell scripting interview questions you can prepare, frequently asked in interviews by tech giants.

1. What are a shell and a shell script?

Answer: Shell is a command interpreter that translates the user's command to the kernel. At the same time, shell scripting is simply a collection of UNIX commands written in plain text format. Instead of specifying a job/command at a time, we use shell scripting to run a list of UNIX commands in a file, similar to a to-do list.

2. Give some advantages and disadvantages of shell script?

Answer: The two main advantages of shell scripting are as follows:

  • It allows you to create your bespoke operating system with the features that best fit your needs.
     
  • It makes it easier to create software applications for specific platforms.
     

The two main disadvantages of shell scripting are as follows:

  • Execution takes place at a relatively lower speed.
     
  • It is not suitable for performing complex tasks.
     

3. Define shell variables and explain the types of variables used in shell scripting.

Answer: Shell variables play a crucial role in shell scripts. Within a shell program, variables allow the shell to store and manipulate data. A variable is a memory location to which we can assign a value. It's a character string to which we can attach a number, text, filename, device, or other value.

There are two types of shell scripting variables, namely:

  1. System-defined variables: The Operating System (Linux) creates and defines system-defined variables. These variables are usually written in capital letters and can be examined using the "set" command.
     
  2. User-defined variables: System users establish or define user-defined variables, and the values of variables can be examined with the command "echo."
     

4. In which file are shell programs stored?

Answer: Shell applications are saved in the sh file.

5. What are the different sorts of shells widely used on a Linux system?

Answer: In the Linux operating system, there are two types of shells: Bourne Shell and C-Shell. The following are some examples of derivatives from each:

Bourne Shell is a shell brand that includes Bourne Shell, Bourne-Again Shell, Korn Shell, and POSIX Shell.

TENEX, C-Shell: TENEX, C-Shell 

6. Define superblock in shell scripting.

Answer: In various operating systems, a Superblock is a metadata collection that displays the filesystems' features. The superblock size, the block size, the empty and filled blocks and their respective counts, the size and position of the inode tables, the disc block map, and use information are all recorded in the superblock.

7. What are features of shell script?

Answer: Here are a few features associated with shell scripts:

  • Automatability: Shell scripts can schedule time-consuming processes, including installing software or backing up files. This can help you save time and work while lowering the possibility of mistakes.
     
  • Portability: Shell scripts can be executed on any system with a compatible shell interpreter, making them portable. They are, therefore perfect for use in dispersed situations or for user sharing.
     
  • Flexibility: A wide range of tasks can be carried out using shell scripts, which offer high flexibility. They can manage files, manage system activities, and even automate complex processes like software development and testing.
     
  • Power: Shell scripts have much potential power, especially when combined with additional tools like variables and regular expressions. Almost any task that can be completed manually in a terminal window may be automated using shell scripts.

8. Explain in brief the four stages of the Linux process.

Answer: Its four stages can be defined as:

  1. Waiting: The Linux process is now waiting for a resource.
     
  2. Running: The Linux process execution takes place.
     
  3. Stopped: The Linux process is made to stop after successful implementation.
     
  4. Zombie: Although the process has ended, it remains active in the process table.
     

9. What benefits does C Shell have over Bourne Shell?

Answer: The C shell supports command aliasing, meaning a user can give a command whatever name he wants. This capability comes in handy when a user must repeatedly type a long command. Instead of entering a long command, a user can input the name he has provided at that moment.

Moreover, a command history functionality is included in the C shell. It remembers the command you typed previously. As a result, you won't have to type the command repeatedly.

10.  Explain the difference between Bourne Shell and C Shell.

Answer: The differences between Bourbe Sell and C Shell are as follows:

  • Syntax:
    For interactive use, Bourne Shell (sh)'s simple and basic syntax is less natural.
    With its more approachable and C-like syntax, C Shell (csh) is more practical for interactive use.
     
  • Capabilities for Scripting:
    Due to its powerful scripting tools, Bourne Shell is used for automation and scripting jobs.
    C Shell is frequently used for interactive operations and as a user shell but is less suitable for complicated scripting.
     
  • History-making Process
    A built-in history system for remembering and reusing earlier commands is absent from Bourne Shell.
    A history feature in C Shell makes it simple for users to recall and reuse earlier commands.
     
  • Customization:
    More manual customisation is needed to configure aliases and the shell environment in Bourne Shell.
    More interactive features and built-in customization possibilities are available in C Shell, including the ability to define aliases on the fly.

11. Is it necessary to use a different compiler to run a shell program?

Answer: The execution of a shell program does not necessitate using a separate compiler. The shell interprets and executes the commands in the shell program.

12.  How many shell scripts are included with the UNIX operating system?

Answer: With the UNIX operating system, there are around 280 shell scripts.

13. When should you avoid using shell programming or scripting?

Answer: In general, shell programming/scripting should be avoided in the situations listed below.

  • When the undertaking is challenging, such as creating a whole payroll processing system.
     
  • When a high level of production is necessary.
     
  • When it necessitates or entails the use of many software tools.
     

14. What are the limitations of shell scripting?

Answer: Here are a few limitations of shell scripting:

  • Errors are common and costly, and even a single can change the command's outcome.
     
  • The rate of execution is slow.
     
  • Bugs or inconsistencies in the syntax or implementation of the language.
     
  • It isn't well suited to large, complicated projects.
     
  • It provides a simple data structure, unlike other scripting languages.
     

15. Define GUI scripting?

Answer: A graphical user interface (GUI) is a method of manipulating a computer and its programs. GUI scripting is helpful for a variety of applications. It is primarily determined by the operating system.

16. Define Crontab.

Answer: Crontab is an abbreviation for Cron Table. It's a list of commands that a user wants to run on a regular basis, as well as the name of the command that keeps track of them. Crontab is the name of the schedule and the program used to change it. It automatically completes tasks for users according to a predetermined timetable.

17. In shell scripting, what is the purpose of the while loop?

Answer: While a command is successfully executed, the while loop is used to execute a series of statements repeatedly. In Shell scripting, the While loop has the following syntax:

while [ condition ]
do
command1
command2
..
….
commandn
done

18. What is the difference between the commands, break and continue?

Answer: Break and Continue commands are defined as:

  • Break: It's a straightforward approach to getting out of a loop that's already started. The break command can break out of any loop, including while and till loops.
     
  • Continue: It just exits the current iteration of the loop, not the entire loop.
     

19.  In Shell Scripting, what is the importance of the Shebang line?

Answer: #!/bin/sh is an example of a Shebang line that appears at the top of a script. It just provides information about where the engine will be installed. The machine is responsible for running the script.

20. Distinguish between $@ and $*.

Answer: $* treats each quotation argument as a separate argument, whereas $@ treats each positional argument as a single string.

21. How long does a shell variable last?

Answer: Only until the conclusion of execution does a variable remain inside the shell script.

22. Tell us how to use a shell script to compare strings.

Answer: The test command is used to compare the text strings. This program compares text strings by comparing each string's characters.

23. Is it possible to distinguish between hard and soft connections in shell scripting? 

Answer: Shell scripting is a robust solution that, like Windows, employs links to construct shortcuts. Soft links have no defined location and can be found anywhere on the same file system. They are usually associated with the file name. On the other hand, hard links remain on the same file system. Hard links are linked to nodes and, in most circumstances, have a specific fixed location.

24. How will you troubleshoot issues with the shell program?

Answer: The following are some common approaches for debugging script issues:

  • Set-x is used to allow debugging.
     
  • In a shell script, use debug statements to display information that will aid in diagnosing the problem.
     

25. What is the state of a process started with the shell's exec command?

Answer: On executing exec, all newly forked processes acquire overlays. In this instance, the command is run without affecting the existing process, and no new process is established.

26. Explain how to use the controls in shell scripting.

Answer: Control instructions in shell scripting describe how the computer will execute specific instructions in a program. They define the control flow in a shell program.

27.  What are control instructions?

Answer: In shell scripting, control instructions are used to manage a script's execution flow. They let you to make choices and take various actions based on the outcomes of those choices.

28. How many types of control instructions are available in a shell?

Answer: In a shell, there are four different categories of control instructions:

  • Sequence control instructions: The execution of the instructions must follow the order in which they occur in the programme.
  • Selection or decision control instructions: These selection or decision control instructions allow the computer to decide which instruction should be carried out next.
  • Loop control instructions: The computer can repeat a series of instructions until a specific condition is met with the help of loop control instructions.
  • Case control instructions: Case control instructions let the computer choose which of a group of instructions to carry out based on the value of a variable.
     

29. What are metacharacters?

Answer: Metacharacters are special characters in a data field or program that convey information about other characters. Regular expressions are what they're called in shells. Metacharacters are non-letter or non-numerical characters that are not letters or numbers. You can use shell metacharacters to group commands, redirect and pipe input/output, run tasks in the background, shorten file and path names, and more. They can be used as wildcards to specify a file name without entirely typing it out. The following are the most prevalent metacharacters:

  • Asterisk(*):  Use the asterisk (*) to match any character.
     
  • Question mark(?): It matches a single character in the filename with a question mark (?).
     
  • Brackets ([...]): The metacharacters used here correspond to certain characters.
     

30. Explain ways to create shortcuts in Linux.

Answer: In Linux, there are several different methods to build shortcuts:

Graphical user interface (GUI): Most Linux desktop environments contain a GUI that enables the creation of shortcuts. Right-click on the desktop or in the file manager and choose "Create shortcut" to accomplish this. The file or programme you want to make a shortcut to can then be chosen.

Terminal: Shortcuts can be made using the terminal as well. To accomplish this, You must create a.desktop file in the /.local/share/applications directory. The following details ought to be in the.desktop file:

[Desktop Entry]
Type=Application
Name=Shortcut Name
Exec=Command to execute shortcut
Icon=Icon path

31. What are the default permissions of a file when it is created?

Answer: The user's umask option determines a file's default permissions when created in shell scripting. A Unix permission mask known as umask regulates the permissions of newly generated files and folders.


Each of the umask's three octal digits corresponds to a different set of permissions for the user, group, and others. Most users' umask default settings are at 022; therefore, newly created files will have the following permissions:

  • User: read and write (6)
  • Group: read (4)
  • Others: none (0)

32. What are the methods for passing and accessing arguments to a script in Linux?

Answer: In Linux, there are several methods for passing and accessing arguments to a script. Here are the most commonly used methods:

1. Positional Parameters

  • Positional parameters are passed to the script as command-line arguments.
     
  • They are accessed using special variables: $1, $2, $3, and so on, where $1 represents the first argument, $2 represents the second argument, and so on.
     
  • Example: `./script.sh arg1 arg2 arg3`
     

2. Special Parameters

  • There are special variables that provide additional information about the script and its arguments.
     
  •  Examples:
    • `$0` - Name of the script itself.
    • `$#` - Number of arguments passed to the script.
    • `$@` - All the positional parameters passed to the script as separate arguments.
    • `$*` - All the positional parameters passed to the script as a single string.
       

3. Getopts

  • Getopts is a built-in utility that allows parsing command-line options and arguments.
     
  • It provides a way to define options and their corresponding actions.
     
  • It is commonly used when scripts need to handle different flags or options.
     
  • Example: `./script.sh -a value1 -b value2`
     

33. What is meant by “s” permission bit in a file?

Answer:  The "s" permission bit, also known as the setuid or setgid bit, has a special meaning when set on a file. The "s" bit can be set in a file's user (owner) or group permissions.

1. Setuid (s) bit

  • When the "s" bit is set in the user (owner) permission of a file, it is called the setuid bit.
     
  • If a file has the setuid bit set, the file will be executed with the permissions of the file owner rather than the permissions of the user executing the file.
     
  • Examples of files with setuid bit: `passwd`, `sudo`, `su`
     

2. Setgid (s) bit:

  • When the "s" bit is set in the group permission of a file, it is called the setgid bit.
     
  • If a file has the setgid bit set, the file will be executed with the permissions of the group owner rather than the permissions of the user executing the file.
     
  • Examples of files with setgid bit: `chmod`, `chgrp`
     

34. What steps would you take to transfer a file from one machine to another?

 Answer: To transfer a file from one machine to another, you can follow these steps

  1. Identify the source and destination machines.
     
  2. Choose one of the file transfer methods from SCP, FTP, SFTP etc.
     
  3. Set up the necessary access and permissions.
     
  4. Transfer the file.
     
  5. Provide authentication and authorization.
     
  6. Verify the transferred file.
     

35. What steps would you take to establish a connection to a database server from a Linux environment?

Answer: You can follow these steps to establish a connection to a database server from a Linux environment.

  1. Install the necessary database client.
     
  2. Obtain the necessary connection details like Hostname or IP address, Port number, Database name, Username and password.
     
  3. Open the terminal.
     
  4. Connect to the database server using specific commands like “mysql -h hostname -P port -u username -p” for MySQL.
     
  5. Enter authentication credentials.
     
  6. Verify the connection.
     

36. Explain the process of configuring an array in Linux.

Answer: Configuring an array typically refers to setting up a software RAID (Redundant Array of Independent Disks) configuration in Linux. RAID allows multiple physical disks to work together as a single logical unit, providing various benefits such as improved performance, data redundancy, or a combination of both. 

Here is a general process for configuring a software RAID array in Linux:

1. Identify and prepare the disks.
 

2. Install the necessary software.

For example, the commonly used software for software RAID in Linux is `mdadm`.
 

3. Create partitions on the disks.
 

4. Create the RAID array.

  • Use the `mdadm` command to create the RAID array.
  • Specify the RAID level (e.g., RAID 0, RAID 1, RAID 5) and the devices (partitions) to include in the array.
  • Example command for creating a RAID 1 array: `mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1`
     

5. Monitor the RAID array.
 

6. Format and mount the RAID array:

  • Format the newly created RAID array with a file system of your choice.
  • Use the `mkfs` command to format the RAID device.
  • Create a mount point (a directory) where you want to access the RAID array and mount it using the `mount` command.
     

7. Test and verify the RAID array:

Frequently Asked Questions

Is shell scripting a skill?

Yes, shell scripting is considered a valuable skill in the field of software development and system administration. Shell scripting involves writing scripts using a command-line interface (CLI) shell to automate tasks on a computer system.

How do I prepare myself for the technical interview round?

Firstly, consider the tools and abilities required for the job. Then, examine valuable books and online resources and prepare for the interview by practising well before.

How can I impress my technical interviewers?

You can impress interviewers by proposing novel solutions or being creative in your responses. You can follow a solution path that few people do or create your solution pattern.

Is shell scripting difficult to understand?

For those who have the prerequisites, shell scripting is not tricky. No familiarity with the underlying commands – it's tough to put together a script if you don't know how to utilize the different tools or even what problems they solve.

How much time is required to learn shell scripting?

It all depends on your understanding and awareness of the manually run shell commands. A script is simply a collection of commands arranged logically for the shell to execute.

Conclusion 

In this article, we have extensively covered all the possible shell scripting interview questions. After these shell scripting interview questions, there is nothing now that can stop you from acing your interview if you have prepared thoroughly and have practised enough.

To read more interview-related blogs on other topics, visit our website, where you can read blogs to enhance your knowledge and have the edge over others.

Recommended Readings:

 

Visit our platform Coding Ninjas Studio to practice top problems, read interview experiences, attempt mock tests, and much more.!

Happy learning, ninjas!

Previous article
Top 30 Pointers interview questions & Answers (2023)
Next article
LWC Lightning Web Component Interview Questions
Live masterclass