Shell Scripting Interview Questions for the Beginners
Here is a list of the top shell scripting interview questions for beginners 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:
-
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.
-
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:
-
Waiting: The Linux process is now waiting for a resource.
-
Running: The Linux process execution takes place.
-
Stopped: The Linux process is made to stop after successful implementation.
-
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.
Shell Scripting Interview Questions for the Intermediates
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.
Advanced Shell Scripting Interview Questions for the Experienced
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
-
Identify the source and destination machines.
-
Choose one of the file transfer methods from SCP, FTP, SFTP etc.
-
Set up the necessary access and permissions.
-
Transfer the file.
-
Provide authentication and authorization.
-
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.
-
Install the necessary database client.
-
Obtain the necessary connection details like Hostname or IP address, Port number, Database name, Username and password.
-
Open the terminal.
-
Connect to the database server using specific commands like “mysql -h hostname -P port -u username -p” for MySQL.
-
Enter authentication credentials.
-
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.
MCQ on Shell Scripting
1. Which of the symbol is used for commenting in a shell script?
A. @
B. $
C. #
D. &
Answer: C. #
2. What is the default shell used in most Linux distributions?
A. Korn Shell
B. Z Shell
C. Bourne Shell
D. Bash Shell
Answer: D. Bash Shell
3. Which command is used to change the permissions of a file in shell scripting?
A. chmod
B. chown
C. change
D. perm
Answer: A. chmod
4. What does the “$?” variable represent in shell scripting?
A. Process ID of the current shell
B. Exit status of the last command
C. Number of background processes
D. User ID
Answer: B. Exit status of the last command
5. How do you define a variable in shell scripting?
A. var = 10
B. var: 10
C. var == 10
D. var=10
Answer: D. var=10
6. Which command is used to display the current working directory in a shell script?
A. pwd
B. dir
C. cd
D. ls
Answer: A. pwd
7. In shell scripting, which command is used to check for file existence?
A. if [ -f filename ]
B. if [ exist filename ]
C. if [ -e filename ]
D. if [ -r filename ]
Answer: C. if [ -e filename ]
8. How do you create an infinite loop in a shell script?
A. while true; do … done
B. while(1) { … }
C. do { … } while(1)
D. loop forever
Answer: A. while true; do … done
9. What is the command used to print text in shell scripting?
A. echo
B. print
C. printf
D. out
Answer: A. echo
10. Which of the following is a valid comparison operator in shell scripting?
A. :=
B. !=
C. ===
D. <>
Answer: B. !=
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 discussed Shell Scripting Interview Questions. Shell scripting remains an essential skill for system administrators, developers, and IT professionals, allowing for efficient automation of tasks and better management of system operations. Whether preparing for a job interview or simply looking to enhance your scripting abilities, understanding the fundamentals and advanced concepts is crucial.
Recommended Readings:
Visit our platform Code360 to practice top problems, read interview experiences, attempt mock tests, and much more.!
Happy learning, ninjas!