A Shell is a command-line interpreter or a complete environment designed to run commands, shell scripts, and programs. Once you feed commands into the shell, it will execute the program based on your input.
Essentially, it translates the input commands and converts them into a Kernel-compatible language. A Shell Script refers to a list of commands in a program run by the Unix Shell. The script includes comments defining the commands in order of their execution sequence.
If the introduction makes you want to know more about Shell Scripting, keep scrolling till the end – we’ve compiled a list of Shell Scripting interview questions and answers to help kickstart your learning process! If you want to learn more about data science, check out our data science courses.
Table of Contents
Shell Scripting Interview Questions & Answers
- What are the advantages of Shell Scripting?
The greatest benefits of Shell Scripting are:
- It allows you to create a custom operating system to best suit your requirements.
- It lets you design software applications based on the platform you’re using.
- It is time-savvy as it helps automate system administration tasks.
2. What are Shell variables?
Shell variables form the core part of a Shell program or script. The variables allow Shell to store and manipulate information within a Shell program. Shell variables are generally stored as string variables.
3. List the types of variables used in Shell Scripting.
Usually, a Shell Script has two types of variables:
- System-defined variables – They are created by the OS(Linux) and are defined in capital letters. You can view them using the Set command.
- User-defined variables – These are created and defined by system users. You can view the variable values using the Echo command.
- How can you make a variable unchangeable?
You can make a variable unchangeable using read-only. Let’s say you want the value of the variable ‘a’ to remain as five and keep it constant, so you use readonly like so:
$ readonly a
- Name the different types of Shells.
There are four core types of Shells, namely:
- Bourne Shell (sh)
- C Shell (csh)
- Korn Shell (ksh)
- Bourne Again Shell (bash)
- Explain “Positional Parameters.”
Positional parameters are variables defined by a Shell. They are used to pass information to the program by specifying arguments in the command line.
- How many Shells and Kernels are available in a UNIX environment?
Typically, a UNIX environment has only one Kernel. However, there are multiple Shells available.
- Do you need a separate compiler to execute a Shell program?
No, you don’t need a separate compiler to execute a Shell program. Since Shell itself is a command-line in the shell program and executes them.
- How do you modify file permissions in Shell Scripting?
You can modify file permissions via umask.
- What does a “.” (dot) at the beginning of a file name indicate?
A file name that starts with a “.” is a hidden file. Usually, when you try to list the files in a Shell, it lists all files except the hidden files. However, the hidden files are present in the directory. If you wish to view hidden files, you must run the Is command with the “–a” flag.
- How to create a shortcut in Linux?
You can create shortcuts in Linux via two links:
Hard link – These links are linked to the inode of the file. They are always present in the same file system as the file. Even if you delete the original file, the hard link will remain unaffected.
Soft link – These links are linked to the file name. They may or may not reside on the same file system as the file. If you delete the original file, the soft link becomes inactive.
12. Name the different stages of a Linux process.
Typically, a Linux process traverses through four phases:
- Waiting – In this stage, the Linux process has to wait for the requisite resource.
- Running – In this stage, the process gets executed.
- Stopped – After successful execution, the Linux process stops.
- Zombie – In the final step, even though the process is no longer running, it remains active in the process table.
- Is there an alternative command for “echo?”
Yes, tput is an alternative for echo command. The tput command allows you to control how the output will be displayed on the screen.
- How many blocks does a file system contain?
A file system has four blocks:
- Superblock – This block offers information on the state of a file system such as block size, block group size, usage information, empty/filled blocks and their respective counts, size & location of inode tables, etc.
- Bootblock – This block holds the bootstrap loader program that executes when a user boots the host machine.
- Datablock – This block includes the file contents of the file system.
- Inode table – UNIX treats all elements as files, and all information related to files is stored in the inode table.
Must Read: Python Interview Questions
- Name the three modes of operation of vi editor.
The three modes of operation are:
- Command mode – This mode treats and interprets any key pressed by a user as editor commands.
- Insert mode – You can use this mode to insert a new text, edit an existing text, etc.
- Ex-command mode – A user can enter all commands at a command line.
- Define “Control Instructions.” How many types of control instructions are available in a Shell?
Control instructions are commands that allow you to specify how the different instructions in a script should be executed. Thus, their primary purpose is to determine the flow of control in a Shell program.
A Shell has four types of control instructions:
- Sequence control instruction enforces the instructions to be executed in the same order in which they are in the program.
- Selection/decision control instruction that enables the computer to determine which instruction should be executed next.
- Repetition/loop control instruction that allows the computer to run a group of statements repetitively.
- Case-control instruction is used when you need to choose from a range of alternatives.
- Define “IFS.”
IFS refers to Internal Field Separator. It is a system variable whose default value is space, tab, following by a new line. IFS denotes where a field or word ends in a line and where another begins.
- Define “Metacharacters.”
A Shell consists of metacharacters, which are special characters in a data field or program that offers information about other characters. For example, the “ls s*” command in a Shell lists all the files beginning with the character ‘s’.
- Differentiate between $* and $@.
While $* treats a complete group of positional parameters as a single string, $@ treats each quoted argument as separate arguments.
Also read: Python Developer Salary in India
21. Write the syntax of while loop in Shell Scripting.
In Shell Scripting, the while loop is used when you want to repeat its block of commands several times. The syntax for the “while” loop is:
while [test condition]
- How are break and continue commands different?
The break command is used to escape out of a loop in execution. You can use the break command to exit from any loop command, including until and while loops. On the other hand, the continue command is used to exit the loop’s current iteration without leaving the complete loop.
23. Why do we use the Shebang line in Shell Scripting?
The Shebang line is situated at the top of a Shell script/program. It informs the user about the location of the engine that executes the script.
Here’s an example of a Shebang line:
- Can you execute multiple scripts in a Shell?
Yes, it is possible to execute multiple scripts in a Shell. The execution of multiple scripts allows you to call one script from another. To do so, you must mention the script’s name to be called when you wish to invoke it.
- Which command should you use to know how long a system has been running?
You need to use the uptime command to know how long a system has been running. Here’s an example of the uptime command:
- Which command should you use to check the disk usage?
You can use the following three commands to check the disk usage:
- df – It is used to check the free disk space.
- du – It is used to check the directory wise disk usage.
- dfspace – It checks the free disk space in megabytes (MB).
27. What do you mean by the Crontab?
Crontab is short for cron table, where Cron is a job scheduler that executes tasks. Crontab is a list of commands you want to run on a schedule, along with the command you want to use to manage that list.
28. When should we not use Shell Scripting?
We shouldn’t use Shell Scripting in these instances:
- If the task is highly complicated, such as writing a complete payroll processing solution, we shouldn’t use Shell Scripting.
- If the job requires a high level of productivity, we shouldn’t use Shell Scripting.
- If the job requires multiple software solutions, we shouldn’t use Shell Scripting.
29. How do you compare the strings in a Shell script?
We use the test command to compare text strings. It compares text strings by comparing every character present in each string.
30. What do you mean by a file system?
A file system is a collection of files along with information related to those files. It controls how the data is retrieved and stored. Without file systems, data present in storage would only be a large body of data with no way of telling where one piece of data ends, and another begins.
31. Can you differentiate between single quotes and double quotes?
Yes. We use single quotes where we don’t want to perform the variables’ evaluation to values. On the other hand, we use double quotes where we want to perform the variables’ evaluation to values.
32. What do you mean by GUI scripting?
We use GUI to control a computer and its applications. Through GUI scripting, we can handle various applications, depending on the operating system.
33. What do you know about the Super Block in Shell scripting?
The Super Block is a program that has a record of particular file systems. It contains characteristics including the block size, filled and empty blocks with their respective counts, the location and the size of the inode tables, usage information, the disk block map, etc.
34. What is the importance of the Shebang line?
The Shebang line remains at the script’s top. It gives information about the location where the engine is, which executes the script.
35. Provide some of the most popular UNIX commands.
Here are some of the most popular UNIX commands:
- cd – The cd command changes the directory to the user’s home directory when used as $ cd. You can use it to change the directory to test through $ cd test.
- ls – The ls command lists the files in the current directory when used as $ ls. You can use it to record files in the long format by using it as $ ls -lrt.
- rm – The rm command will delete the file named fileA when you use it as $ rm fileA.
- cat – This command would display the contents present in a file when you use it as $ cat filename.
- mv – The mv command can rename or move files. For example, the $ mv fileA fileB command would move files named fileA and fileB.
- date – The date command shows the present time and date.
- grep – The grep command can search for specific information in a file. For example, the $ grep Hello fileA command would search for the lines where the word ‘Hello’ is present.
- finger – The finger command shows information about the user.
- ps – The ps command shows the processes presently running on your machine.
- man – The man command shows the online help or manual about a specified command. For example, the $ ms rm command would display the online manual for the rm command.
- pwd – The pwd command shows the current working directory.
- wc – The wc command counts the number of characters, words, and lines present in a file.
- history – The history command shows the list of all the commands you used recently.
- gzip – The gzip command compresses the specified file. For example, the $ gzip fileA command would compress fileA and change it into fileA.gz.
- logname – The logname command would print the user’s log name.
- head – The head command shows the first lines present in the file. For example, the $ head -15 fileA command would display the first 15 lines of fileA.
Additional Notes: This one is among the most crucial Shell scripting interview questions. We recommend preparing a more thorough list of UNIX commands as many versions of this question are asked in interviews.
Must Read: Data Science Interview Questions
36. How is C Shell better than Bourne Shell?
C Shell is better than Bourne Shell for the following reasons:
- C Shell lets you alias the commands. This means the user can give any desired name to a command. It is quite beneficial when the user has to use a lengthy command multiple times. Instead of typing the command’s long name numerous times, the user can type the assigned name. It saves a lot of time and energy, making the process much more efficient.
- C Shell has a command history feature, where C Shell remembers all the previously used commands. You can use this feature to avoid typing the same command multiple times. It enhances efficiency substantially.
Due to the above two reasons, using C Shell is much more advantageous than Bourne Shell.
37. What is it essential to write Shell Scripts?
Shell scripting has many benefits that make it crucial. It takes input from users, files it, and displays it on the screen. Moreover, it allows you to make your own commands and automate simple daily tasks. You can use Shell scripting to automate system administration tasks also. Shell scripting makes your processes more efficient by saving you a lot of energy and time. Due to this, it is quite essential and widely used.
We hope these 26 Shell Scripting interview questions and answers help you break the ice on Shell Scripting and prepare for your next interview!
If you are curious to learn about data science, check out IIIT-B & upGrad’s Executive PG Programme in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.
What is shell scripting?
Shell scripting is the process of writing scripts/codes to automate monotonous tasks. Instead of putting in the commands one after the other n times, you can build a script to automate a series of instructions that run one after the other. The essential stages in shell scripting are writing the script, making it accessible to the shell, and granting the shell execution authority. Shell scripts are written in ASCII text and include ASCII text. They are created with a text editor, word processor, or Graphical User Interface (GUI). The script's content consists of a sequence of commands written in a language that the shell can understand. Once completed, the file is saved with an extension and at a place accessible to the shell.
What are the applications of shell scripting?
Shell scripting is easy and effective. It utilizes the same syntax in the script as it would on the shell command line, eliminating any ambiguity. A shell script is handy for repetitive activities that would take too long to complete by entering one line at a time. Users can use shell scripts for a variety of purposes like automating the process of compiling code, running a program or establishing a programming environment, completing the batch and manipulating files, as well as connecting existing programs, carrying out routine backups, and monitoring systems.
Is shell scripting a good profession?
Shell scripting is used in various fields like automation, programming, system administration chores, application automation, package setup, validation automation, data backup and restoration in a system, etc. So, Shell scripting is an essential part of the IT department of every company. Also, it is helpful throughout an individual's IT career. With an average pay of ₹6,39,000 per year, Shell scripting is a good profession with many prospects for its learners. Most IT professionals use it every day for modest automated chores as it saves a lot of time.