Shell Scripting Interview Questions and Answers

1. What is the Shell Script and why it is required?
Answer: A Shell Script is a text file that contains one or more commands. As a system administrator, we often need to issue several commands to accomplish the task, we can add these all commands together in a text file (Shell Script) to complete daily routine tasks.

2. What is the basic syntax of while loop in shell scripting?
Answer: Like the for loop, the while loop repeats its block of commands several times. Unlike the loop, however, the while loop iterates until its while condition is no longer true.

3. Basic Syntax of do-while statement?
Answer: The do-while statement is similar to the while statement but performs the statements before checking the condition statement.

4. What are the disadvantages of shell scripting?
Answer: There are many disadvantages of shell scripting they are

Design flaws can destroy the entire process and could prove a costly error.
Typing errors during the creation can delete the entire data as well as partition data.
Initially, the process is slow but can be improved.
Portability between different operating systems is a prime concern as it is very difficult to port scripts etc.

5. Explain About Login Shell?
Answer: Login shell is very useful as it creates an environment that is very useful to create the default parameters. It consists of two files they are profile files and shell RC files. These files initialize the login and nonlogin files. Environment variables are created by the Login shell.

6. What are the various stages of a Linux process it passes through?
Answer: A Linux process generally passes through four stages:
Waiting: In this stage, the Linux process waits for the resource.
Running: In this stage, the Linux process is currently being executed.
Stopped: In this stage, the Linux process is stopped after successful execution.
Zombie: This stage is known as a zombie because the process has stopped but still active in the process table.

7. What are the signals?
Answer: Signals are software interrupts. Signal provides a way of handling asynchronous events: a user at a terminal typing the interrupt key to stop a program or the next program in the pipeline terminating prematurely.

8. What is ‘ps’ command for?
The ps command prints the process status for some or all of the running processes. The information given is the process identification number (PID), the amount of time that the process has taken to execute so far, etc.

9. What Is The Use Of Break Command?
The break command is a simple way to escape out of a loop in progress. We can use the break command to exit out from any loop, including while and until loops.

10. How to debug a shell script?
 A shell script can be debugged if we execute the script with the ‘-x’ option ( sh -x Another way to debug a shell script is by using the ‘-new’ option ( sh -NV

11. What is the syntax of while loop in shell scripting?
Answer: The while loop is used to repeat its block of commands a number of times. It is different from for loop, it iterates until the while condition is no longer true.

12. What is Shell?
Answer: Shell is a command interpreter, which interprets the command which the user gives to the kernel. It can also be defined as an interface between a user and an operating system.

13. What are control instructions and how many types of control instructions are available in a shell?
Answer: Control Instructions are the ones, which enable us to specify the order in which the various instructions in a program/script are to be executed by the computer. Basically, they determine a flow of control in a program.

There are 4 types of control instructions that are available in a shell.

Sequence Control Instruction – This ensures that the instructions are executed in the same order in which they appear in the program.
Selection or Decision Control Instruction – It allows the computer to take a decision as to which instruction is to be executed next.
Repetition or Loop Control Instruction – It helps a computer to execute a group of statements repeatedly.
Case-Control Instruction – This is used when we need to select from several alternatives.

14. Explore About Environment Variables?
Answer: Environment variables are set at the login time and every shell that starts from this shell gets a copy of the variable. When we export the variable it changes from a shell variable to an environment variable and these variables are initiated at the start of the shell.

15. What are the types of permission at the file level in Shell?
Answer: Owner permissions: The permissions granted to a user with all the rights at the root level are known as Owner permissions.

Group permissions:  The permissions granted to a user with all the rights in a particular group are known as Owner permissions.

Other (world) permissions: The permissions granted to a user with all the rights globally to a file system are known as Owner permissions.

16. What is IFS??
Answer: IFS stands for Internal Field Separator. And it is one of the system variables. By default, its value is space, tab, and a new line. (Shell Scripting Interview Questions and Answers)

17. Explain in brief about sed command with an example?
Answer: sed stands for stream editor. And it is used for editing a file without using an editor. It is used to edit a given stream i.e. a file or input from a pipeline.

18. How can you calculate the overall number of arguments that have been passed to a script? Why it is important?
Answer: This can easily be done by a command $#. The same is required to know the overall load on a script. Every limit has a limit on handling the same and users have to check it often to make sure the script remains in the running mode and doesn’t get fail.

19. In Shell scripting, how the user can frequently monitor a log file that is updating?
Answer: There is a concept of tailing in the shell scripting that can be applied for this. By using the tail-f filename this can be done. Actually, it will enable users to display the previous 10 lines on the output. The same reflects the part of the file which is updating continuously.

20. How the break and continue in the system are different from one another in scripting?
Answer: When a: Break” is used within a shell scripting loop is considered, it simply terminates the entire loop. On the other side, the “Continue” will make only the current iteration to terminate. However, you can have the loop available with you in the next iteration. These are the two powerful and in fact very useful features when it comes to constructing the shell.

21. Are you familiar with the methods that are useful in debugging a problem related to the shell script?
Answer: The answer to this question depends on the skills of a professional. There are various methods and it is not always necessary that all users consider a similar method for problem debugging. Also, it actually depends on the problem itself.

The first method that can help users to formulate the problem is simply inserting some debug statements. This will actually help the pinpoint to know how and where the problem was declared itself or reported. Another method that is also common and the users can make use of it is set-x. 

22. What is GUI scripting?
Answer: GUI stands for Graphical User Interface. It is used for controlling a computer and its applications. GUI scripting supports different applications. It mostly depends on the operating system. (Shell Scripting Interview Questions and Answers

23. What is UNIX?
Answer: It is a portable operating system that is designed for both efficient multi-tasking and multi-user functions. Its portability allows it to run on different hardware platforms. It was written in C and lets the user do processing and control under a shell.

24. Explain About The Slow Execution Speed Of Shells?
Answer: The major disadvantage of using shell scripting is the slow execution of the scripts. This is because for every command a new process needs to be started. This slow down can be resolved by using pipeline and filter commands. A complex script takes a much longer time than a normal script.

25. Explain About Echo Command?
Answer: The Echo command is used to display the value of a variable. There are many different options give different outputs such as usage \c suppress a trailing line, \r returns a carriage line, -e enables interpretation, \r returns the carriage.

26. Is it possible to pass arguments to the scripts when the same in running in the UNIX?
Answer: Yes, it is possible and this practice is followed often. It is done to make sure that the system doesn’t have any error while executing he commands and the scripts are running smoothly. There are other rea0sons as well for which it can be executed.

27. In UNIX shell scripting, what there is a need to connect the system to a remote server according to you?
Answer: This is done generally when it comes to executing some special commands and the users are free to get this done through various commands. This simply lets users get an additional control or to get some additional support or help. This is actually considered as one of the effective ways for the same. However, this approach is to be secured with an authentication process that can be a simple username and password.

28. What are Vi Editor modes available?
Answer: While working with Vi Editor, different operations we perform are done in different modes.

Command Mode – Launching Vi automatically starts command mode.
Edit Mode – This mode provides an environment to edit text.
Ex Mode – Vi editor interaction is made available with file processing instructions.

29. What are the advantages of shell scripting?
Answer: These are the two main advantages of shell scripting:

It facilitates you to develop your operating system with relevant features best suited for you.
You can design software applications according to their platform. 

30. How are devices represented in UNIX?
Answer: All devices are represented by files called special files that are located in/dev directory. Thus, device files and other files are named and accessed in the same way. A ‘regular file’ is just an ordinary data file in the disk.

31. How to make variables as unchangeable?
Answer: Variables can be made unchangeable using readonly. For instance, if we want to variable a value to remain as 10 and not to be changed then we can achieve this using readonly.

32. What is the command to be used to display the shell’s environment variables?
Answer: Command to be used to display the shell’s environment variables is env or printenv.

33. What is the function of utilities that come with the open client driver in shell scripting?
Answer: They are useful for connecting the system with a database server simply. The users have no performed this task to accomplish so many tasks that are relevant and important in scripting. Scripting needs a lot of data and information and it is not always possible that the same is kept at a particular location only. The users have to make sure of an error-free outcome in this approach.

34. Why is the use of shell script?
Answer: It is used by system administrators to issue numbers of commands to accomplish the task. All commands are added together in a text file (shell script) to complete daily routine tasks.

35. Give Some Situations Where Typing Error Can Destroy A Program?
Answer: There are many situations where typing errors can prove to be a real costly effort. For example, a single extra space can convert the functionality of the program from deleting the subdirectories to files deletion. cp, cn, cd all resemble the same but their actual functioning is different.

36. What is the difference between diff and CMP commands?
Answer: diff – Basically, it tells about the changes which need to be made to make files identical.
CMP – it compares two files byte by byte and displays the very first mismatch.

37. Explain About Non-login Shell Files?
Answer: The nonlogin shell files are initialized at the start and they are made to run to set up variables. Parameters and paths can be set etc are some important functions. These files can be changed and also your own environment can be set. These functions are present in the root. It runs the profile each time you start the process.

38. What is the session?
Answer: A Session is a collection of one or more process groups. A process establishes a new session by calling setsid function. This function return process groupid ok.

39. Basis of the shell program relies on what fact?
Answer: The basis of shell programming relies on the fact that the UNIX shell can accept commands not just only from the keyboard but also from a file.

40. Can you tell something about the Zombie Processes in shell scripting scripting
Answer: These are generally defined as the scripts that have completed their life span but are yet to be picked by parent processes associated with them. In the process table, the users can locate the process id of the same despite it remains nonfunctional. In some special cases, it is possible to provide the same id to the next scripts or processes when the functions performed.

Note:  BrowseLatest Shell Scripting Interview Questions and Shell Scripting Tutorials Here you can check. Shell Scripting Training details and Shell Scripting videos for self learning. Contact +91 988 502 2027 for more information.

Leave a Comment