Shell Scripting Interview Questions And Answers

TOP 100+ Shell Scripting Interview Questions And Answers

TOP 100+ Shell Scripting Interview Questions And Answers

Shell Scripting Interview Questions And Answers are as follows –

1: What is a shell?

Shell is an interface between the user and the kernel. Even though there can be only one kernel; a system can have many shell running simultaneously. So, whenever a user enters a command through the keyboard, the shell communicates with the kernel to execute it and then display the output to the user.

2: What are the different types of commonly used shells on a typical Linux system?

csh,ksh,bash,Bourne . The most commonly used and advanced shell used today is “Bash” .

3: What is the equivalent of a file shortcut that we have a window on a Linux system?

Shortcuts are created using “links” on Linux. There are two types of links that can be used namely “soft link” and “hard link”.

4: What is the difference between soft and hard links?

Soft links are link to the file name and can reside on different filesytem as well; however hard links are link to the inode of the file and have to be on the same filesytem as that of the file. Deleting the original file makes the soft link inactive (broken link) but does not affect the hard link (Hard link will still access a copy of the file)

5: How will you pass and access arguments to a script in Linux?

Arguments can be passed as:

scriptName “Arg1” “Arg2″….”Argn” and can be accessed inside the script as $1 , $2 .. $n

6: What is the significance of $#?

$# shows the count of the arguments passed to the script.

7: What is the difference between $* and $@?

$@ treats each quoted arguments as separate arguments but $* will consider the entire set of positional parameters as a single string.

8: Use sed command to replace the content of the file (emulate tac command)


if cat fille



Then O/p should be


sed ‘1! G; h;$!d’ file1

Here G command appends to the pattern space,

h command copies pattern buffer to hold buffer

and d command deletes the current pattern space.

9: Given a file, replace all occurrence of word “ABC” with “DEF” from 5th line till end in only those lines that contains word “MNO”

sed –n ‘5,$p’ file1|sed ‘/MNO/s/ABC/DEF/’

10: Given a file, write a command sequence to find the count of each word.

tr –s  “(backslash)040” <file1|tr –s  “(backslash)011″|tr “(backslash)040 (backslash)011” “(backslash)012” |uniq –c

where “(backslash)040” is octal equivalent of “space”

“(backslash)011” is an octal equivalent of “tab character” and

“(backslash)012” is an octal equivalent of the newline character.

11: How will you find the 99th line of a file using only tail and head command?

tail +99 file1|head -1

12: Print the 10th line without using tail and head command.

sed –n ’10p’ file1

13: In my bash shell I want my prompt to be of format ‘$”Present working directory”:”hostname”> and load a file containing a list of user-defined functions as soon as I log in, how will you automate this?

In bash shell, we can create “.profile” file which automatically gets invoked as soon as I log in and write the following syntax into it.

export PS1=’$ `pwd`:`hostname`>’ .File1

Here File1 is the file containing the user-defined functions and “.” invokes this file in current shell.

14: Explain about “s” permission bit in a file?

“s” bit is called “set user id” (SUID) bit.

“s” bit on a file causes the process to have the privileges of the owner of the file during the instance of the program.

For example, executing “passwd” command to change current password causes the user to writes its new password to shadow file even though it has “root” as its owner.

15: I want to create a directory such that anyone in the group can create a file and access any person’s file in it but none should be able to delete a file other than the one created by himself.

We can create the directory giving read and execute access to everyone in the group and setting its sticky bit “t” on as follows:

mkdir direc1


chmod g+wx direc1


chmod +t direc1

16: How can you find out how long the system has been running?

We can find this by using the command “uptime”.

17: How can any user find out all information about a specific user like his default shell, real-life name, default directory, when and how long he has been using the system?

finger “loginName” …where loginName is the login name of the

user whose information is expected.

18: What is the difference between $$ and $!?

$$ gives the process id of the currently executing process whereas $! Shows the process id of the process that recently went into the background.

19: What are zombie processes?

These are the processes which have died but whose exit status is still not picked by the parent process. These processes even if not functional still have its process id entry in the process table.

20: How will you copy a file from one machine to other?

We can use utilities like “ftp,” “scp” or “rsync” to copy a file from one machine to other.

E.g., Using ftp:

FTP hostname

>put file1


Above copies, file file1 from the local system to destination system whose hostname is specified.

21: I want to monitor a continuously updating log file, what command can be used to most efficiently achieve this?

We can use tail –f filename. This will cause only the default last 10 lines to be displayed on std o/p which continuously shows the updating part of the file.

22: I want to connect to a remote server and execute some commands, how can I achieve this?

We can use ssh to do this:

ssh username@serverIP -p sshport


ssh root@ -p 22

Once above command is executed, you will be asked to enter the password

23: I have 2 files and I want to print the records which are common to both.

We can use “comm” command as follows:

comm -12 file1 file2 … 12 will suppress the content which are

unique to 1st and 2nd file respectively.

24: Write a script to print the first 10 elements of Fibonacci series.





echo $a

echo $b

for I in 1 2 3 4 5 6 7 8





echo $b


25: How will you connect to a database server from Linux?

We can use isql utility that comes with open client driver as follows:

isql –S serverName –U username –P password

26: What are the 3 standard streams in Linux?

0 – Standard Input1 – Standard Output2 – Standard Error

27: I want to read all input to the command from file1 direct all output to file2 and error to file 3, how can I achieve this?

command <file1 1>file2 2>file3

28: What will happen to my current process when I execute a command using exec?

“exec” overlays the newly forked process on the current process; so when I execute the command using exec, the command gets executed on the current shell without creating any new processes.

E.g., Executing “exec ls” on command prompt will execute ls and once ls exits, the process will shut down

29: How will you emulate wc –l using awk?

awk ‘END {print NR} fileName’

30: Given a file find the count of lines containing the word “ABC”.

grep –c “ABC” file1

31: What is the difference between grep and egrep?

egrep is Extended grep that supports added grep features like “+” (1 or more occurrence of a previous character),”?”(0 or 1 occurrence of a previous character) and “|” (alternate matching)

32: How will you print the login names of all users on a system?

/etc/shadow file has all the users listed.

awk –F ‘:’ ‘{print $1}’ /etc/shadow|uniq -u

33: How to set an array in Linux?

Syntax in ksh:

Set –A arrayname= (element1 element2 ….. element)

In bash

A=(element1 element2 element3 …. elementn)

34: Write down the syntax of “for ” loop


for  iterator in (elements)


execute commands


35: How will you find the total disk space used by a specific user?

du -s /home/user1 ….where user1 is the user for whom the total disk space needs to be found.

36: Write the syntax for “if” conditionals in Linux?


If  condition is successful


execute commands


execute commands


37: What is the significance of $?

The command $? gives the exit status of the last command that was executed.

38: How do we delete all blank lines in a file?

sed  ‘^ [(backslash)011(backslash)040]*$/d’ file1

where (backslash)011 is an octal equivalent of space and

(backslash)040 is an octal equivalent of the tab

39: How will I insert a line “ABCDEF” at every 100th line of a file?

sed ‘100i\ABCDEF’ file1

40: Write a command sequence to find all the files modified in less than 2 days and print the record count of each.

find . –mtime -2 –exec wc –l {} \;

41: How can I set the default rwx permission to all users on every file which is created in the current shell?

We can use:

umask 777

This will set default rwx permission for every file which is created for every user.

42: How can we find the process name from its process id?

We can use “ps –p ProcessId”

43: What are the four fundamental components of every file system on Linux?

Bootblock, super block, inode block and Datablock are found fundamental components of every file system on Linux.

44: What is a boot block?

This block contains a small program called “Master Boot record”(MBR) which loads the kernel during system boot up.

45: What is a super block?

Super block contains all the information about the file system like the size of file system, block size used by its number of free data blocks and list of free inodes and data blocks.

46: What is an inode block?

This block contains the inode for every file of the file system along with all the file attributes except its name.

47: How can I send a mail with a compressed file as an attachment?

zip file1|mailx –s “subject” Recipients email id

Email content


48: How do we create command aliases in a shell?

alias Aliasname=”Command whose alias is to be created”.

49: What are “c” and “b” permission fields of a file?

“c ” and “b” permission fields are generally associated with a device file. It specifies whether a file is a special character file or a block special file.

50: What is the use of a shebang line?

Shebang line at the top of each script determines the location of the engine which is to be used to execute the script.

 Q51. List some of the common and most widely used UNIX commands.

Answer: Given below is a list of widely used UNIX Commands.

Command Example/Usage of Command Description
ls 1. $ ls
2. $ ls –lrt or $ ls -ltr
1. It lists files in the current directory.
2. It lists files in the long format.
cd 1. $ cd
2. $ cd test
3. $ cd .. (after cd space needs to be given before entering two dots.)
1. It changes directory to your home directory.
2. It changes directory to test.
3. It moves back to one directory or to the parent directory of your current directory.
mkdir $ mkdir test It creates a directory called test.
rmdir $ rmdir test1
CAUTION: Be careful while using this command.
It removes directory test1.
cp 1. $ cp file1 test
2. $ cp file1 file1.bak
1. It copies file1 to test directory.
2. It takes backup of file1.
rm $ rm file1
CAUTION: Be careful while using this command.
It removes or deletes a file1.
mv $ mv file1 file2 It moves or renames file1 to file2.
more $ more It checks or display one page at a time.
touch $ touch test It creates an empty file called test.
cat 1. $ cat File1
2. $ cat test1 > test2
1. It displays contents of File1.
2. It creates a new file test2 with the contents of test1.
compress $ compress file1 It reduces the size of file1 and creates a compressed file called file1.z and deletes file1.
date $ date
e.g. Output:
Tuesday, September 12, 2017 06:58:06 AM MDT
It displays current date and time.
diff $ diff file1 file2 It displays line by line difference between file1 and file2.
find $ find . –name ‘*.t’ -print It searches in the current directory and in all its subdirectories for files ending with .t, and writes their
names in the output.
finger $ finger It displays information about user.
who $ who It lists the users those who are logged in on the machine.
grep 1.$ grep Hello file1
2.$ grep –c Hello file1
1. It searches for the lines containing Hello in file1.
2. It gives count or number of lines that contains Hello in file1.
kill kill
$ kill 1498
It kills the process which is having PID as 1498.
lpr 1.$ lpr –Pprinter1 test
2.$ lp file1
1. It sends file test to print it on printer1.
2. It prints file1.
man $ man ls It displays online manual or help about ls command.
passwd $ passwd It is used to change the password.
pwd $ pwd
e.g. Output: /u/user1/Shell_Scripts_2017
It displays present working directory.
ps $ ps
e.g. Output:
1498 3b 0:10 sh
1500 3b 0:05 sh
It displays the list of processes which are currently running on the machine.
talk $ talk user1 It is used to talk to the user1 who is currently logged into the same machine.
wc $ wc file1
e.g. Output:
4 6 42 file1
It counts the number of lines, words and characters in file1.
chmod $ chmod 744 file1 It changes the permissions of file1 & assigns this permission rwxr–r–
gzip $ gzip file1 It compresses the file1. After compression file1 should look like this, file1.gz
gunzip $ gunzip file1.gz It uncompresses the file1.gz. After uncompression file1.gz should look like this, file1
history $ history It lists all the commands which are recently used.
logname $ logname
e.g. Output:
It prints log name of the user.
uname $ uname
e.g. Output:
It gives information about unix system which you are using.
tty $ tty
e.g. Output:
It displays the device name of your terminal.
sort $ sort file1 This will sort the contents of file1 and displays sorted output on the screen.
head $ head -15 file1 It displays first 15 lines of the file.
tail $ tail -15 file1 It displays last 15 lines of the file.
  1. What are the default permissions of a file when it is created?

Answer: 666 i.e. rw-rw-rw- is the default permission of a file, when it is created.

  1. What are the two types of Shell Variables? Explain in brief.

Answer: The two types of shell variables are:

#1) UNIX Defined Variables or System Variables – These are standard or shell defined variables. Generally, they are defined in CAPITAL letters.

Example: SHELL – This is a Unix Defined or System Variable, which defines the name of the default working shell.

#2) User Defined Variables – These are defined by users. Generally, they are defined in lowercase

  1. How variables can be wiped out?

Ans: Variables can be wiped out or erased using the unset command.

$ a =20
$ unset a

Upon using the above command the variable ‘a’ and its value 20 get erased from shell’s memory.

CAUTION: Be careful while using this unset command.

  1. What are positional parameters? Explain with an example.

Answer: Positional parameters are the variables defined by a shell. And they are used whenever we need to convey information to the program. And this can be done by specifying arguments at the command line.

There is a total of 9 positional parameters present i.e. from $1 to $9.

Example: $ Test Indian IT Industry has grown very much faster
In the above statement, positional parameters are assigned like this.
$0 -> Test (Name of a shell program/script)
$1 ->Indian
$2 -> IT and so on.

  1. What does the. (dot) indicate at the beginning of a file name and how should it be listed?

Answer: A file name that begins with a. (dot) is called as a hidden file. Whenever we try to list the files it will list all the files except hidden files.

But, it will be present in the directory. And to list the hidden file we need to use –a option of ls. i.e. $ ls –a.

  1. Explain about file permissions.

Answer: There are 3 types of file permissions as shown below:

Permissions Weight
r – read 4
w – write 2
x – execute 1

The above permissions are mainly assigned to owner, group and to others i.e. outside the group. Out of 9 characters first set of 3 characters decides/indicates the permissions which are held by the owner of a file. The next set of 3 characters indicates the permissions for the other users in the group to which the file owner belongs to.

And the last 3 sets of characters indicate the permissions for the users who are outside the group. Out of the 3 characters belonging to each set, the first character indicates the “read” permission, the second character indicates “write” permission and the last character indicates “execute” permission.

Example: $ chmod 744 file1

This will assign the permission rwxr–r–to file1.

  1. What are the different blocks of a file system? Explain in brief.

Answer: Given below are the main 4 different blocks available on a file system.

File System  
Block No. Name of the Block
1st Block Boot Block
2nd Block Super Block
3rd Block Inode Table
4th Block Data Block
  • Super Block: This block mainly tells about a state of the file system like how big it is, maximum how many files can be accommodated, etc.
  • Boot Block: This represents the beginning of a file system. It contains the bootstrap loader program, which gets executed when we boot the host machine.
  • Inode Table: As we know all the entities in a UNIX are treated as files. So, the information related to these files is stored in an Inode table.
  • Data Block: This block contains the actual file contents.
  1. What are the three different security provisions provided by UNIX for a file or data?

Answer: Three different security provisions provided by UNIX for a file or data are:

  • It provides a unique user id and password to the user, so that unknown or unauthorized person should not be able to access it.
  • At the file level, it provides security by providing read, write & execute permissions for accessing the files.
  • Lastly, it provides security using file encryption. This method allows encoding a file in an unreadable format. Even if someone succeeds in opening a file, but they cannot read its contents until and unless it is decrypted
  1. What are the three modes of operation of vi editor? Explain in brief.

Answer: The three modes of operation of vi editors are,

  1. Command Mode: In this mode, all the keys pressed by a user are interpreted as editor commands.
  2. Insert Mode: This mode allows for the insertion of a new text and editing of an existing text etc.
  3. The ex-command Mode: This mode allows a user to enter the commands at a command line.


  1. In Shell scripting, how you will put separate the grep and egrep?

Grep can easily be extended and the same can then be called as egrep. In other words, the egrep is an advanced version of grip. There are some added features in it and i.e. it can easily be considered for additional occurrence of a previous character. This can also be considered when it comes to alternate matching.

  1. How you will create shortcut in the Linux?

This can be done with the help of links present in the UNIX. For this, basically there are two links which are considered often. They are generally categorized as

Soft Link

Hard Link

63. What is Super Block in Shell scripting?

It is basically a program that contains all the information regarding a specific file system. It reflects the block size that is used by its associated number, the size of system in terms of data handling and programming. It also provides information regarding the free inodes and the data blocks which are currently associated with the system.

  1. In Shell scripting, how you will display the process id of the process executing currently and id of processes that take place recently?

For this, there are actually two dedicated commands which can be executed directly and they are $$ and $.

  1. What are Zombie Processes in shell scripting?

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 non functional. In some special cases, it is possible to provide the same id to the next scripts or processes when the functions performed are similar.

  1. In shell scripting, what is the significance of the Shebang line?

It simply provides information regarding the location where the engine is placed. The engine is the one that execute the script. The Shebang line is present at the top of the script and it can be neglected by the users if they want the same.

  1.  How can you put separate soft and the hard link in shell scripting? 

Well, shell scripting is a powerful approach. The links are used when it comes to creating shortcuts just like Windows. Soft links are those which generally related to the file name and they don’t have any specific location. They can be anywhere on the file system. On the other side, the hard links are related to the node and have a particular location which is fixed in most of the cases. They remain present on the same file system.

  1. Is it possible to use a shell script to determine if the directory actually exists or not?

Yes, this is possible and for this, the users are free to simply consider the UNIX test command. The option that is useful in the –d option. It is not always necessary that the information regarding the existence of a directory is displayed only when the directory is recognized by the system. Information regarding all the directories whether known to the system or not can be displayed with this command. Generally directories are present in the variable $mydir.

  1. Where exactly you can store the Shell programs in the system?

They are stored in a file which is tagged as Sh (Bourne Shell)

  1. Why C Shell is better than Bourne Shell?
  • All the commands can be aliased simply with the C shell whereas the same is not possible in case of Bourne Shell
  • Lengthy commands can be used again and again in C shell whereas the Bourne doesn’t allow the same in all the cases
  • The command history can be accessed through the C shell but it cannot be accessed through the Bourne
  • There is no need to type the command again and again in case of C
  1. How do I print all arguments submitted on a command line?

echo $@ or echo $*

  1. What are the advantages and disadvantages of shell scripting?

Advantages of shell scripting:

  1. Can design applications (software) according to their platform.
  2. To run a sequence of commands as a single command.
  3. Portable (It can be executed in any Unix-like operating systems without any modifications)

Disadvantages of shell scripting:

Slow execution speed compared to any programming languages

When a tying error occurs during the creation then it will delete the entire data as well as partition data.

  1. What are the difference between &/&&?

& – “Bitwise AND”, evaluates both sides of the operation.
&& – “Logical AND Operator”, evaluates at the left side of the operation (If it is True) it continues at the right side.

  1. Which command helpful to forward errors to a file?

The command that used for forwarding error to a file is 2> filename

  1. What Is The Difference Between $* And $@?
  • $* – It treats the whole set of positional parameters as Single String
  • $@ – This considers every quoted argument as independent or separate arguments.
  1. Difference between process and thread

To put it in simple words, a thread is a small piece of code/instruction that needs to be executed in a process, whereas a process is a collection of one or more thread executions to complete one complete task.

Note: A process consists of multiple threads can share resources among them as they belong to the same process. And threads belong to different processes cannot share their resources.

  1. Why do we use “$?” in shell scripting?

This command returns the exit status of the previously executed command.

0 for successful execution, and non-zero for failure.

  1. What is “$*” in shell scripting?

Arguments list passed in the current process.

  1. What is the command for number of arguments passed in command line arguments?

$#” is the command.

  1. How can you get the PID of the current process?

$$” is used.

81.  What is GUI scripting?

GUI is used for controlling a computer and its applications. GUI scripting supports different applications. It mostly depends on the operating system.

82. What is the lifespan of a variable inside a shell script?

The lifespan of a variable inside shell script is only until the end of execution.

83. What does it mean by #!/bin/sh or #!/bin/bash at the beginning of every script?

script may specify #!/bin/bash on the first line, meaning that the script should always be run with bash, rather than another shell. /bin/sh is an executable representing the system shell. Actually, it is usually implemented as a symbolic link pointing to the executable for whichever shell is the system shell.

84. What is the Crontab?

Crontab stands for cron table because it uses the job scheduler cron to execute tasks. The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list.

The schedule is called the crontab, which is also the name of the program used to edit that schedule.

85. How many fields are present in a crontab file and what does each field specify?

The crontab file has six fields.

The first five fields contain information on when to execute the command and they are as follows;

  • minute(0-59)
  • hour(0-23)
  • day(1-31)
  • month(1-12)
  • day of the week(0-6, Sunday = 0).

The sixth field contains the command to be executed.

86. What are the two files of crontab command?

The two files of crontab command are:

  • cron.allow which decides the users need to be permitted for using the crontab command.
  • cron.deny which decides the users need to be prevented from using the crontab command.

87. What are the different commands available to check the disk usage?

There are three different commands available 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 is used to check the free disk space in terms of MB.

88. What are the different communication commands available in the Shell?

There are four different communication commands available in Shell.

  • mail
  • news
  • wall
  • motd

89. How to debug the problems encountered in the shell script/program?

Given below are some common methods used to debug the problems in the script.

  • Debug statements can be inserted in the shell script to output/display the information which helps to identify the problem.
  • Using set -x we can enable debugging in the script.
  1. What are control instructions and how many types of control instructions are available in a shell? Explain in brief.

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 the 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.
  1. What are Loops and explain three different methods of loops in brief?

Answer: Loops are the ones, which involve repeating some portion of the program/script either a specified number of times or until a particular condition is being satisfied.

3 methods of loops are:

  • For Loop: This is the most commonly used loop. For loop allows specifying a list of values that the control variable in the loop can take. The loop is then executed for each value mentioned in the list.
  • While Loop: This is used in a program when we want to do something for a fixed number of times. While loop gets executed until it returns a zero value.
  • Until Loop: This is similar to while loop except that the loop executes until the condition is true. Until the loop gets executed at least once, it returns a non-zero value.
  1. 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. It signifies that in a line where one field or word ends and another begins.

  1. How to open a read-only file in Unix/shell?

Answer: Read-only file can be opened by:
vi –R <File Name>

  1. 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 – Basically it compares two files byte by byte and displays the very first mismatch.

95. What is bash script?

The bash script is a shell programming language. Generally, we run many types of shell commands from the terminal by typing each command separately that require time and efforts. If we need to run the same commands again then we have to execute all the commands from the terminal again. But using a bash script, we can store many shell command statements in a single bash file and execute the file any time by a single command. Many system administration related tasks, program installation, disk backup, evaluating logs, etc. can be done by using proper bash script.

96. What are the advantages of using bash scripts?

Bash script has many advantages which are described below:

  • It is easy to use and learn.
  • Many manual tasks that need to run frequently can be done automatically by writing a bash script.
  • The sequence of multiple shell commands can be executed by a single command.
  • Bash script written in one Linux operating system can easily execute in other Linux operating system. So, it is portable.
  • Debugging in bash is easier than other programming languages.
  • Command-line syntax and commands that are used in the terminal are similar to the commands and syntax used in bash script.
  • Bash script can be used to link with other script files.

97. Mention the disadvantages of bash scripts

Some disadvantages of bash script are mentioned below:

  • It works slower than other languages.
  • The improper script can damage the entire process and generate a complicated error.
  • It is not suitable for developing a large and complex application.
  • It contains less data structure compare to other standard programming languages.

98. What are the 3 standard streams in Linux?

0 – Standard Input

1 – Standard Output

2 – Standard Error

  1. What is nohup in UNIX?

Nohup is a special command which is used to run process in background, but it is slightly different than & which is normally used for putting a process in background. An UNIX process started with nohup will not stop even if the user who has stared log off from system. While background process started with & will stop as soon as user logoff.

  1. What is ‘ps’ command for?

Answer: The ps command prints the process status for some or all of the running processes. The information given are the process identification number (PID),the amount of time that the process has taken to execute so far etc.

Related Posts:

For more Interview Questions And Answers click here