Unix Shell Scripting Interview Questions and Answers

Unix Shell Scripting Interview Questions and Answers

Last updated on 25th Sep 2020, Blog, Interview Question

About author

RamaKrishnan (Sr Technical Architect )

High level Domain Expert in TOP MNCs with 12+ Years of Experience. Also, Handled Around 36+ Projects and Shared his Knowledge by Writing these Blogs for us.

(5.0) | 15028 Ratings 849

Shell scripting or programming mostly consists of the features which today’s modern programming languages offer.

Right from simple to complex script can be developed using Shell Scripting. It is nothing but a series of UNIX commands written in a plain text file to accomplish a specific task. And also with the help of shell scripting, tasks of the day to day life can be automated.

There are hardly a few documents available over the internet on shell scripting interview questions and answers. Hence, I have chosen Shell Scripting as my topic to help those who are in need of it.

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

Ans:

It simply provides information regarding the location where the engine is placed. The engine is the one that executes 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

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

Ans:

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

3.What are the different types of commonly used shells on a typical Linux system?

Ans:

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

4.What are zombie processes?

Ans:

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.

5.What is the importance of writing Shell Scripts?

Ans:

The points given below explain the importance of writing shell scripts.

Shell script takes input from the user, file and displays it on the screen.

Shell scripting is very useful in creating your own commands.

It is helpful in automating some tasks of the day to day life.

It is useful for automating system administration tasks.

Mainly it saves time.

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

Ans:

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

7.What are signals?

Ans:

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 Shell?

Ans:

A shell acts as an interface between the user and the system. As a command interpreter, the shell takes commands and sets them up for execution.

9.What are Shell Variables?

Ans:

Shell variables are the main part of shell programming or scripting. They mainly provide the ability to store and manipulate information within a shell program.

10.How many Shell Scripts come with the Unix Operating System?

Ans:

There are approximately 280 shell scripts that come with the UNIX operating system.

Subscribe For Free Demo

Error: Contact form not found.

11.In Shell scripting, how can you say that C Shell is better than Bourne Shell?

Ans:

  • 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

12.When should we use the “if” condition (before)?

Ans:

When running several commands the “if” condition meets.

13.Is a separate compiler required for executing a Shell Program?

Ans:

A separate compiler is not required to execute a shell program. The shell itself interprets the command in the shell program and executes them.

14.How you will make the soft link inactive in the scripting

Ans:

The soft links can simply be made inactive in your delete file and the same doesn’t have any impact on the hard link and i.e. the users can access the file copy.

15.What is the difference between soft and hard links?

Ans:

Soft links are linked to the file name and can reside on different filesystems as well; however hard links are linked to the inode of the file and have to be on the same file system 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)

16.How Many Fields Are Present In A Crontab File And What Does Each Field Specify?

Ans:

The crontab file has six fields. The first five fields tell cron when to execute the command: minute(0-59), hour(0-23), day(1-31), month(1-12), and day of the week(0-6, Sunday = 0).

And the sixth field contains the command to be executed

17.What are the different types of Shells available?

Ans:

There are mainly 4 important types of shells that are widely used.

And they include:

  • Bourne Shell (sh)
  • C Shell (csh)
  • Korn Shell (ksh)
  • Bourne Again Shell (bash)

18.What is the significance of $#?

Ans:

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

19.What is Unix Shell?

Ans:

A Unix shell is a command-line interpreter that provides a Unix-like command line user interface.

20.What command can be used to test if $a greater than 12?

Ans:

The following command is used:

  • [ $a -gt 12 ]

21.What is the difference between $$ and $!?

Ans:

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

22.What is Shell Scripting?

Ans:

Shell Scripting is a collection of commands put together into a file. Script is a command or an instruction given to process and these set of instructions put together in a file to perform some task.

23.Shell Programs Are Stored In Which File?

Ans:

Shell programs are stored in a file called sh.

24.How To Accomplish Any Task Via Shell Script?

Ans:

Any task can be accomplished via shell script at the dollar ($) prompt and vice versa.

25.Basis Of Shell Program Relies On What Fact?

Ans:

The basis of shell programming relies on the fact that UNIX shells can accept commands not just only from the keyboard but also from a file.

26. How do you create a shortcut in Linux?

Ans:

This can be done with the help of links present in Linux OS.

Hard Link: Hard links are linked to the inode of the file and have to be on the same file system as of the file. Deleting the original file does not affect the hard link.

Soft Link: Soft links are linked to the file name and can reside on a different file system as well. Deleting the original file makes the soft link inactive.

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

Ans:

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

28.What are the advantages of C Shell over Bourne Shell?

Ans:

The advantages of C Shell over Bourne Shell are:

  • C shell allows aliasing of commands i.e. a user can give any name of his choice to the command. This feature is mainly useful when a user has to type the lengthy command again and again. At that point of time, instead of typing a lengthy command a user can type the name that he has given.
  • C shell provides a command history feature. It remembers the previously typed command. Thus, it avoids typing the command again and again.

29. In a typical UNIX environment how many kernels and shells are available?

Ans:

In a typical UNIX environment, only one kernel and many shells are available.

30.When should shell programming/scripting not be used?

Ans:

Generally, shell programming/scripting should not be used in the below instances.

  • When the task is very much complex like writing the entire payroll processing system.
  • Where there is a high degree of productivity required.
  • When it needs or involves different software tools.
Course Curriculum

Best Advanced Unix Shell Scripting Training from MNC Experts

  • Instructor-led Sessions
  • Real-life Case Studies
  • Assignments
Explore Curriculum

31.Basis of the shell program relies on what fact?

Ans:

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.

32.What are the default permissions of a file when it is created?

Ans:

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

33.What can be used to modify file permissions?

Ans:

File permissions can be modified using umask.

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

Ans:

The two types of shell variables are:

    1. #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.

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

Example:

  • $ a=10 –Here the user has defined a variable called ‘a’ and assigned value to it as 10.

35.How are shell variables stored? Explain with a simple example.

Ans:

Shell variables are stored as string variables.

Example:

  • $ a=10

In the above statement a=10, the 10 stored in ‘a’ is not treated as a number, but as a string of characters 1 and 0.

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

Ans:

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

37.How to make variables unchangeable?

Ans:

Variables can be made unchangeable using readonly. For instance, if we want the variable ‘a’ value to remain as 10 and not change, then we can achieve this using readonly.

Example:

  • $ a=10
  • $ readonly a

38.How variables can be wiped out?

Ans:

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

Example:

  • $ a =20
  • $ unset a

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

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

Ans:

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.

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

Ans:

A filename that begins with a. (dot) is called 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 –an option of ls. i.e. $ ls –a.

41.Generally, each block in UNIX is how many bytes?

Ans:

Each block in UNIX is 1024 bytes.

42.By default, a new file and a new directory that is being created will have how many links?

Ans:

New file contains one link. And a new directory contains two links.

43.Explain about file permissions.

Ans:

There are 3 types of file permissions as shown below:

PermissionsWeight
r – read4
w – write2
x – execute1

The above permissions are mainly assigned to owner, group and to others i.e. outside the group. Out of 9 characters the 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.

44.What is a file system?

Ans:

The file system is a collection of files that contain related information of the files.

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

Ans:

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

File System 
Block No.Name of the Block
1st BlockBoot Block
2nd BlockSuper Block
3rd BlockInode Table
4th BlockData Block
  • Super Block: This block mainly tells about the 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.

46.What are the three different security provisions provided by UNIX for a file or data?

Ans:

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

47.What are the three editors available in almost all the versions of UNIX?

Ans:

The three editors are ed, ex & vi.

48.What are the three modes of operation of vi editor? Explain in brief.

Ans:

The three modes of operation of vi editors are,

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

49.What is the alternative command available to echo and what does it do?

Ans:

tput is an alternative command to echo.

Using this, we can control the way in which the output is displayed on the screen.

50.How to find out the number of arguments passed to the script?

Ans:

The number of arguments passed to the script can be found by the below command.

  • echo $ #
Course Curriculum

Enroll in Unix Shell Scripting Certification Course By Experts Trainers

Weekday / Weekend BatchesSee Batch Details

51.What are control instructions and how many types of control instructions are available in a shell? Explain in brief.

Ans:

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.

52.What are Loops and explain three different methods of loops in brief?

Ans:

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.

53.What is IFS?

Ans:

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.

54.What is a Break statement and what is it used for?

Ans:

The break is a keyword and is used whenever we want to jump out of a loop instantly without waiting to get back to the control command.

When the keyword break is encountered inside any loop in the program, control will get passed automatically to the first statement after a loop. A break is generally associated with an if.

55.What is a Continue statement and what is it used for?

Ans:

Continue is a keyword and is used whenever we want to take control of the beginning of the loop, by passing the statements inside the loop which have not yet been executed.

When the keyword Continue is encountered inside any loop in the program, control automatically passes to the beginning of the loop. Continue is generally associated with an if.

56.What are Metacharacters in a shell? Explain with some examples.

Ans:

Metacharacters are special characters in a program or data field which provides information about other characters. They are also called regular expressions in a shell.

Example:

  • ls s* – It lists all the files beginning with character ‘s’.
  • Execution over Shell Interpreter/Editor
Execution-over

output:

execution-shell-over
  • $ cat script1 > script2 – Here output of cat command or script1 will go to a script2.
  • Execution over Shell Interpreter/Editor
comment-or-script

Output:

bin-bath-usages-script
  • $ ls; who – This will execute ls first and then who.
  • Execution over Shell Interpreter/Editor
meta-character-script-output

Output:

executing-multiple-script
tibadm-crmadm

57.How to execute multiple scripts? Explain with an example.

Ans:

In a shell, we can easily execute multiple scripts i.e. one script can be called from the other. We need to mention the name of a script to be called when we want to invoke it.

Example:

In the below program/script upon executing the first two echo statements of script1, shell script executes script2. Once after executing script2, the control comes back to script1 which executes a pwd command and then terminates.

Code for script1

executing-script

Code for script2

software-testihg-is-an-interesting

Execution of script1 over Shell Interpreter/Editor

output-displayed-on-the-editor

Output displayed on the Editor upon executing script1

interesting-job-on-software

58.Which command needs to be used to know how long the system has been running?

Ans:

uptime command needs to be used to know how long the system has been running.

Example:

  • $ uptime

On entering the above command at shell prompt i.e. $ uptime, the output should look like this.

  • 9:21am  up 86 day(s), 11:46,  3 users,  load average: 2.24, 2.18, 2.16

Execution over Shell Interpreter/Editor

uptime-shell-script

Output:

current-shell

59.How to find the current shell which you are using?

Ans:

We can find the current shell that we are using with echo $SHELL.

Example:

  • $ echo $SHELL

Execution over Shell Interpreter/Editor

our-system

Output:

Keyboard-inputs

60.How to find all the available shells in your system?

Ans:

We can find all the available shells in our system with $ cat /etc/shells.

Example:

  • $ cat /etc/shells

Execution over Shell Interpreter/Editor

available-shell-output

Output:

shell-scripts

61.How to read keyboard inputs in shell scripts?

Ans:

Keyboard inputs can be read in shell scripts as shown below,

Script/Code

Script-Code

Execution over Shell Interpreter/Editor

Interpreter-Editor

Output:

keywork-input-script

62.What are the two files of crontab command?

Ans:

Two files of crontab command are:

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

63.What command needs to be used to take the backup?

Ans:

tar is the command which needs to be used to take the backup. It stands for tape archive. The tar command is mainly used to save and restore files to and from an archive medium like tape.

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

Ans:

There are three different commands available to check the disk usage.

They are:

  • df – This command is used to check the free disk space.
  • du – This command is used to check the directory wise disk usage.
  • dfspace – This command is used to check the free disk space in terms of MB.

65.What are the different communication commands available in Unix/Shell?

Ans:

Basically, there are 4 different communication commands available in Unix/Shell. And they are mail, news, wall & motd.

66.How to find out the total disk space used by a specific user, say for example username is John?

Ans:

The total disk space used by John can be found out as:

  • du –s/home/John

67.What is Shebang in a shell script?

Ans:

Shebang is a # sign followed by an exclamation i.e. !. Generally, this can be seen at the beginning or top of the script/program. Usually, a developer uses this to avoid repetitive work. Shebang mainly determines the location of the engine which is to be used in order to execute the script.

Here ‘#’ symbol is called hash and ‘!’ is called a bang.

Example:

  • #!/bin/bash

The above line also tells which shell to use.

68.What is the command to be used to display the shell’s environment variables?

Ans:

Command to be used to display the shell’s environment variables is env or printenv.

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

Ans:

Though it depends on the type of problem encountered. 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.

70.How to know the variable length?

Ans:

Variable length can be checked by

  • $ {#variable}
Unix Shell Scripting Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

71.What is the difference between = and ==?

Ans:

  • = -> This is used for assigning value to the variable.
  • == -> This is used for string comparison.

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

Ans:

Read-only file can be opened by:

  • vi –R <Filename>

73.How can the contents of a file inside the jar be read without extracting in a shell script?

Ans:

The contents of the file inside a jar can be read without extracting in a shell script as shown below.

  • tar –tvf <Filename>.tar

74.What is the difference between diff and cmp commands?

Ans:

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.

75.Explain in brief about sed command with an example.

Ans:

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.

Syntax:

  • sed options file

Example:

Execution over Shell Interpreter/Editor

command-present

Here ‘s’ command present in sed will replace string Hello with Hi.

Output:

calculate-number-of-passed

76.How to calculate the number of passed arguments ?

Ans:

  • $#

77.How to get the script name inside a script ?

Ans:

  • $0

78.How to check if previous commands run successfully ?

Ans:

  • $?

79.How to get the last line from a file ?

Ans:

  • tail -1

80.How to get the first line from a file ?

Ans:

  • head -1

81.How to get the 3rd element from each line from a file ?

Ans:

  • awk ‘{print $3}’

82.How to get 2nd element from each line from a file, if first equal FIND

Ans:

  • awk ‘{ if ($1 == “FIND”) print $2}’

83.How to debug bash script

Ans:

  • Add -xv to #!/bin/bash

84. What are the different types of variables used in Shell Script?

Ans:

 A shell script has two types of variables :

  • System-defined variables are created/defined by the Operating System(Linux) itself. These variables are generally defined in Capital Letters and can be viewed by the “set” command.
  • User-defined variables are created or defined by system users and the values of variables can be viewed by using the command “echo”.

85.What is a boot block?

Ans:

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

86.What is a super block?

Ans:

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

87.What is an inode block?

Ans:

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

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

Ans:

  • #!/bin/sh
  • a=1
  • b=1
  • echo $a
  • echo $b
  • for I in 1 2 3 4 5 6 7 8
  • do
  • c=a
  • b=$a
  • b=$(($a+$c))
  • echo $b
  • done

89.How will you connect to a database server from Linux?

Ans:

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

  • isql –S serverName –U username –P password

90.What are the 3 standard streams in Linux?

Ans:

  • 0 – Standard Input1 – Standard Output2 – Standard Error

91.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?

Ans:

  • command <file1 1>file2 2>file3

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

Ans:

  • sed ‘100i\ABCDEF’ file1

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

Ans:

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

Are you looking training with Right Jobs?

Contact Us

Popular Courses