Shell Scripting Interview Questions and Answers

50+ TOP Shell Scripting Interview Questions and Answers

Shell Scripting Interview Questions
Shell Scripting Interview Questions

1) What is shell scripting?
Shell scripting is used to program command line of an operating system. Shell Scripting is also used to program the shell which is the base for any operating system. Shell scripts often refer to programming UNIX. Shell scripting is mostly used to program operating systems of windows, UNIX, Apple, etc. Also this script is used by companies to develop their own operating system with their own features.

2) State the advantages of Shell scripting?
There are many advantages of shell scripting some of them are, one can develop their own operating system with relevant features best suited to their organization than to rely on costly operating systems. Software applications can be designed according to their platform.

3) What are the disadvantages of shell scripting?
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 process is slow but can be improved.
* Portbility between different operating system is a prime concern as it is very difficult to port scripts etc.

4) Explain about the slow execution speed of shells?
Major disadvantage of using shell scripting is 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 much longer time than a normal script.

5) Give some situations where typing error can destroy a program?
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 sub directories to files deletion. cp, cn, cd all resemble the same but their actual functioning is different. Misdirected > can delete your data.
Coding Related Shell Scripting Interview Questions …

6) Explain about return code?
Return code is a common feature in shell programming. These return codes indicate whether a particular program or application has succeeded or failed during its process. && can be used in return code to indicate which application needs to be executed first.

7) What are the different variables present in Linux shell?
Variables can be defined by the programmer or developer they specify the location of a particular variable in the memory. There are two types of shells they are System variables and user defined variables. System variables are defined by the system and user defined variables are to be defined by the user (small letters).

8) Explain about GUI scripting?
Graphical user interface provided the much needed thrust for controlling a computer and its applications. This form of language simplified repetitive actions. Support for different applications mostly depends upon the operating system. These interact with menus, buttons, etc.

9) Explain about echo command?
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.

10) Explain about Stdin, Stdout and Stderr?
These are known as standard input, output and error. These are categorized as 0, 1 and 2. Each of these functions has a particular role and should accordingly functions for efficient output. Any mismatch among these three could result in a major failure of the shell.

11) Explain about sourcing commands?
Sourcing commands help you to execute the scripts within the scripts. For example sh command makes your program to run as a separate shell. .command makes your program to run within the shell. This is an important command for beginners and for special purposes.

12) Explain about debugging?
Shell can make your debugging process easier because it has lots of commands to perform the function. For example sh –ncommand helps you to perform debugging. It helps you to read the shell but not to execute it during the course. Similarly sh –x command helps you by displaying the arguments and functions as they are executed.

13) Explain about Login shell?
Login shell is very useful as it creates an environment which 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 non login files. Environment variables are created by Login shell.

14) Explain about non-login shell files?
The non login shell files are initialized at the start and they are made to run to set up variables.
Parameters and path 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.

15) Explain about shebang?
Shebang is nothing but a # sign followed by an exclamation. This is visible at the top of the script and it is immediately followed by an exclamation. To avoid repetitive work each time developers use shebang. After assigning the shebang work we pass info to the interpreter.

16) Explain about the Exit command?
Every program whether on UNIX or Linux should end at a certain point of time and successful completion of a program is denoted by the output 0. If the program gives an output other than 0 it defines that there has been some problem with the execution or termination of the problem.
Whenever you are calling other function, exit command gets displayed.

17) Explore about Environment variables?
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 an shell variable to an environment variable and these variables are initiated at the start of the shell.

18. What is Shell’s Responsibilities ?
The shell is responsible for the execution of all programs that you request from your terminal. Each time you type in a line to the shell, the shell analyzes the line and then determines what to do.The line that is typed to the shell is known more formally as the command line. The shell scans this command line and determines the name of the program to be executed and what arguments to pass to the program.

19. What is “$#” Variable ?
The $# Variable
Whenever you execute a shell program, the special shell variable $# gets set to the number of arguments that were typed on the command line.

20. Explain “Exit Status” for a shell script ?
Whenever any program completes execution under the Unix system, it returns an exit status back to the system. This status is a number that usually indicates whether the program successfully ran. By convention, an exit status of zero indicates that a program succeeded, and nonzero indicates that it failed. Failures can be caused by invalid arguments passed to the program, or by an error condition detected by the program. For example, the cp command returns a nonzero exit status if the copy fails for some reason (for example, if it can’t create the destination file), or if the arguments aren’t correctly specified (for example, wrong number of arguments, or more than two arguments and the last one isn’t a directory). In the case of grep, an exit status of zero (success) is returned if it finds the specified pattern in at least one of the files; a nonzero value is returned if it can’t find the pattern or if an error occurs (the arguments aren’t correctly specified, or it can’t open one of the files).

21. What is “Command Substitution” ?
Command substitution is the process by which the shell runs a command and replaces the command substitution with the output of the executed command. That sounds like a mouthful, but it’s pretty straightforward in practice.

22. What is ” eval” command ?
The eval command exists to supersede the normal command-line substitution and evaluation order, making it possible for a shell script to build up commands dynamically. This is a powerful facility, but it must be used carefully. Because the shell does so many different kinds of substitutions, it pays to understand the order in which the shell evaluates input lines.

23. What is awk ?
An awk invocation can define variables, supply the program, and name the input files.

24. What is “grep” programe ?
The grep program is the primary tool for extracting interesting lines of text from input datafiles. POSIX mandates a single version with different options to provide the behavior traditionally obtained from the three grep variants: grep, egrep, and fgrep.

25. Name a new feature introduced with PHP 5.
PHP 5 introduces (among other things) SQLite support, improved XML support, and a significantly improved object model.

26. Explain “read” command ?
The read command is one of the most important ways to get information into a shell program:
$ x=abc ; printf “x is now ‘%s’. Enter new value: ” $x ; read x
x is now ‘abc’. Enter new value: PDQ
$ echo $x
PDQ

27. What are the two files used by the shell to initialize itself?
/etc/profile
profile

28. What is Interactive mode?
Interactive mode means that the shell expects to read input from you and execute the commands that you specify. This mode is called interactive because the shell is interacting with a user. This is usually the mode of the shell that most users are familiar with: you log in, execute some commands, and log out. When you log out using the exit command, the shell exits.

29 What is noninteractive mode?
In this mode, the shell does not interact with you; instead it reads commands stored in a file and executes them. When it reaches the end of the file, the shell exits.

30. what is local variable?
A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. The variables that you looked at previously have all been local variables.

31. What is environment variable?
An environment variable is a variable that is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually a shell script defines only those environment variables that are needed by the programs that it runs.

32. What is shell variable?
A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly. Some of these variables are environment variables whereas others are local variables.

33. Explain the “Exit” command?
Every program whether on UNIX or Linux should end at a certain point of time and successful completion of a program is denoted by the output 0. If the program gives an output other than 0 it defines that there has been some problem with the execution or termination of the problem. Whenever you are calling other function, exit command gets displayed.

34. How do you find out what’s your shell?
echo $SHELL

35. How you will run a process in the background?
./ProcessName &

36. How do you write a while loop in shell?
Use While Loop

37. How do you read keyboard input in shell scripts?
Use read command

38. What is GUI Scripting?
Graphical user interface provided the much needed thrust for controlling a computer and its applications. This form of language simplified repetitive actions. Support for different applications mostly depends upon the operating system. These interact with menus, buttons, etc.

39. Explain the term “loops”?
Loops enable you to execute a series of commands multiple times. Two main types of loops are the while and for loops.

40. What is “Nested Loops”?
When a loop is located inside the body of another loop it is said to be nested within another loop.

41. What is “Infinite Loops”?
Loops that execute forever without terminating.

42. What is “File Descriptor”?
An integer that is associated with a file. Enables you to read and write from a file using the integer instead of the file’s name.

43. Explain “STDIN”?
STDIN Standard Input. User input is read from STDIN. The file descriptor for STDIN is 0.

44. Explain “STDOUT”?
STDOUT Standard Output. The output of scripts is usually to STDOUT. The file descriptor for STDOUT is 1.

45. Explain “STDERR”?
STDERR Standard Error. A special type of output used for error messages. The file descriptor for STDERR is 2.

46. Explain “Escape Sequence”?
An escape sequence is special sequence of characters that represents another character.

47. Explain “Output Redirection” in shell scripting?
In UNIX or Linux, the process of capturing the output of a command and storing it in a file is called output redirection because it redirects the output of a command into a file instead of the screen.

48. Explain “Input Redirection” in shell scripting?
In UNIX or Linux the process of sending input to a command from a file is called input redirection.

49. What is “Field separator”?
The field separator controls the manner in which an input line is broken into fields. In the shell, the field separator is stored in the variable IFS. In awk, the field separator is stored in the awk variable FS.

50. What is “Library”?
A file that contains only functions is called a library. Usually libraries contain no main code.

Latest Shell Scripting Interview Questions