20 Unix Interview Questions and Answers - Freshers, Experienced

Dear Readers, Welcome to Unix interview questions with answers and explanation. These 20 solved Unix questions will help you prepare for technical interviews and online selection tests conducted during campus placement for freshers and job interviews for professionals.

After reading these tricky Unix questions, you can easily attempt the objective type and multiple choice type questions on Unix.

There are multiple shell types of Unix. What are the features of the Bourne and Korn shell?

The Bourne shell is the standard shell of Unix. It provides the following features:

- It gives the ability of input/output redirection.
- Allows the usage of metacharacters for file name abbreviations.
- The environment can be customized using the shell variables.
- It provides the user with a built in command set for the creation of shell programs.
- Allows the user to control a job.

The Korn shell is an extension of the Bourne shell which is backward compatible. The feature unique to the Korn shell are as follows:

- Allows the user to do command line editing.
- It maintains a command history, which enables the user to check the last commands executed.
- There is a provision for integer arithmetic.
- Provides the support for arrays and arithmetic expressions.
- Give the user the option to use aliases which is used to abbreviate the command name.

What do you understand by File modes in Unix?

Unix enables the user to set the desired level of privacy. It allows the user to set the access permission of the file. This is know as the file mode of a file.

The file permissions of file are displayed in the form :

drwxrwxrw filename date time

The letters r means read, w means write and x means execute.

When the permission of a file is set by the user it is necessary to classify the users into three categories:

- Owner: the user who creates the file.
- Group: other users having the same permissions as the group owner.
- other: all other users.

In the text drwxrwxrw the d letter signifies the directory.
Following the d letter the next three letters specify the permissions of the owner. the next three specify the permissions of the group. The last three characters specify the permissions of the other users.

What is the alias mechanism in UNIX?

The alias command allows the user to specify or assign another name to a command. It can be also used to club together a group of commands.

For ex.

alias dir ‘ls -sFC’

The above command on being executed would tell the shell that dir is another name for the command ls -sFC. In this way, the user can simply assign an alias name to a command that could be difficult to remember. Now the user simply needs to use the specified alias name as given by him in place of the command for the same effect as the code.

The user can check the number of aliases active by typing in the command from the shell:

uhunix% alias pwdls ‘pwd; ls -sFC’

What are the different ways of redirecting I/O?

Unix allows the user to redirect the output of an operation instead of sending it to your screen. The user has to make used of the redirection pointer (> & >>). The various ways of redirecting the output are as follows:

- To copy the contents of one file to another:

uhunix% cat file1> file2

- To concatenate multiple files:

uhunix% cat file1 file2 file3 > newfile

- To append a file to another file:

uhunix% cat bottomfile >> appendeefile

- To redirect the input from instead of the keyboard to a file:

uhunix% mail username < letterfile

What are the uses of pipes in UNIX?

Piping in unix allows the user to set the output of a specific operation as input to another. The piping of output from one file to another is performed by using the | symbol.

For ex.

uhunix% w | grep username

This command would allow the user to check if the specified user is logged in or not. When pipes are used in combination with redirection it gives the user the powerful ability to manipulate long operations in short steps.

For ex.

uhunix% ls | grep vi > vi.files

This command would result in the creation of a vi.files which would consist of the names of all the files in the current directory with a vi in their names.

What steps does the shell take after processing a command.

Once a command line is terminated by the key the shell continues to process the command line in passes.

- Parsing: The for the shell is to separate the commands line into words. For this it uses the references of spaces and delimiters specified by the user. Any consecutive spaces are replaced by a single space unless specified.

- Identifying variables: Any word preceded by a $ sign are treated as variables unless specified so.

- Substitution: Any command surrounded by back quotes are executed by the shell and replaces the command with the output given.

- Wild card recognition: After all the above tasks are completed the shell looks for wild card symbols in the command line.

What are the possible return values of kill()?

The kill() method on execution can yield the following possible results:

- ‘0’ returned: Means that the process exists with the specified PID. It will allow the user to send signals to this process.

- ‘-1’ returned, ‘errno==ESRCH’: This means that the process with the specified PID does not exist or some security enhancements is denying its existence.

- ‘-1’ returned, ‘errno==EPERM’: This means that the system would not permit the process to be killed.
The EPERM process is used to detect if a process exists or not. Any other error would specify that the process does not exist.

What is the super block in UNIX?

The file system of a system is described by a system block. Right at the beginning the super block is built when the file system is being created. The purpose of the super block is to contain the basic parameters of a system, for eg, the number of blocks and a count of the total number of files.

There are two superblocks:

- The default super block is always present at a fix offset from the beginning of the system`s disk partition.
- The redundant super block is not usually referenced unless the default superblock is effected by some error or system crash.

The file system consists of files. Some files may be categorized as directories which in itself contains the pointers to files that are themselves directories or files. Every file has a descriptor known as the inode. The inode describes the ownership information of a file.

How can non printable characters be shown in unix?

A file sometimes may contain non printable characters such as line breaks for windows. Most of the editors used with unix do not display these non-printable characters. In case the user wants to view these characters he has to use the vim -b command to see and edit these characters. The -b flag used after vim is used to force the vim to use the binary mode.

For ex.

% cat foo.txt

% vim -b foo.txt

What are the types of path names that can be used in Unix?

In a file system which contains a hierarchy of directories the user has to specify a path to a file or directory to access it.

For ex.


The above path refers to the file text.notes inside the folder which is inside the lyrics folder. The lyrics folder is contained by the music folder which in turn is contained by the my folder. The root directory is home.

There are two types of paths that can be used in Unix:

- Absolute Path name: This type of file name starts with the "/" symbol i.e. the root.
- Relative Path name: This path name begins from the current working directory where the user is.

For the above stated path the relative directory would be:


Write the list of commands that can used in relation to users in unix

The various commands that can be used to know about the user information in unix are as follows:

- id Show the active user id with login and group
- last Shows last logins on the system
- who Shows who is logged on the system
- groupadd admin Used to add group "admin" and user colin (Linux/Solaris)
- usermod -a -G Used to add existing user to group (Debian)
- usermod -A Used to add existing user to group (SuSE)
- adduser raj FreeBSD add user raj (interactive)

These commands can be executed at the shell and are distribution dependent.

How can file permissions be set in unix?

The command to change file permissions in unix is ‘chmod’ or also known as the change mode. With this command the user can set the permissions for users on how to access a file and what can be done with it.

The syntax for changing file permissions is:

chmod [who] [+/-] [new-permissions] [file]

The "who" can be referred to:

- u : This is for the user who owns the file.
- g: This is meant to apply to the group which owns this file.
- o: This is used to specify the permissions for the rest of the users.
- a: This is used to specify everyone and anyone.

The +/- sign refer to if the permissions are to added or removed. The new permissions can be used in form of characters (r for read , x for execute) etc.

How is conditional execution used in UNIX?

Conditional execution can be accomplished in UNIX with the use of control operators “and” and “or”. The and operator is represented as && and the syntax of their usage is :

command1 && command2

This implies that the command 2 will only be executed if the command 1 returns an exit status 0.

The or operator is represented by ||. The syntax of using the or operator is:

command1 || command2

This implies that command2 is executed if the command 1 returns a non-zero exit value.

Both the operators can be used together.

For ex.

command1 && command2 if exist status is zero || command3 if exit status is non-zero

This implies that command 2 will be executed if and only if command 1 executes or else command 3 will be executed.

Write a script to print numbers 5,4,3,2,1 using the while loops.

The algorithm for writing such a script would be:

- Begin value of i is set to 5.
- The while loop is started.
- Check of the value of i is zero. In case it is it will end the program and give the output.
- In case, the i is not equal to zero the value of i will be printed and the i's value is decremented by 1.

The code:

while test $i != 0
echo "$i"
i=`expr $i - 1`

Write the algorithm and code to display the sum of all the digits of a number?

The algorithm for the above requirement would be:

- The user is asked enter the number n.
- The sum and sd is set to zero at the beginning.
- Find the single digit in sd as n%10 which will give the left most digit.
- Construct the sum as sum = sum +sd
- The n is decremented by 1.
- If n is zero print the sum else repeat the loop.

The code for the above algorithm:

if [ $# -ne 1 ]
echo "Usage: $0 number"
echo " I will find sum of all digit for given number"
echo " For eg. $0 123, I will print 6 as sum of all digit (1+2+3)"
exit 1
while [ $n -gt 0 ]
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
echo "Sum of digit for number is $sum"

Write the script to define a background process that will continuously print time at upper right corner of the screen.

The script to enable the user to work in the shell while defining a background program to display the time at the upper right screen is as follows:

echo "Th clock"
echo "Use kill PID to dicontinue using this block"
echo "Press a key to continue. . ."

while :
ti=`date +"%r"`
echo -e -n "\033[7s" #save current screen position & attributes

tput cup 0 69 # row 0 and column 69 is used to show clock

echo -n $ti # put clock on screen

echo -e -n "\033[8u" #restore current screen position & attributes
#Delay for 1 second
sleep 1

Write the script to display a number in reverse order? Also specify the algorithm.

The algorithm for the above program would be:

- Ask the user to enter the number.
- Set the values of rev and sd as 0.
- Find the single digit that is the left most digit.
- Construct the new number as rev * 10 + sd
- Now the n has to be decremented by 1.
- In case n is greater then zero repeat the above steps from third step or else print the number.

The code for the above algorithm is:

if [ $# -ne 1 ]
echo "Usage: $0 number"
echo "I will find reverse of given number"
echo "For eg. $0 123, I will print 321"
exit 1


while [ $n -gt 0 ]
sd=`expr $n % 10`
rev=`expr $rev \* 10 + $sd`
n=`expr $n / 10`
echo "Reverse number is $rev"

How can shell scripts be debugged in Unix?

A user when programming in shell may face an error. The user can make use of the -v and -x option with sh or bash command to debug the shell script.

The syntax format of using the option is:

sh option {shell-script-name} or
bash option {shell-script-name}

The option in the above syntax can be either -v or -x. The -v option prints the shell input lines as they are read. The -x option on the other hand expands a simple command and along with it displays the value of the ps4 variable followed by the command. It also shows the complete list of expanded arguments.

How can a users custom environment be made permanent?

Unix provides the user with the option to make their custom environment permanent so that the user does not have to redefine the variables of the environment again in order to define a new process. The user needs to place the set and setnv commands into certain special files to make the environment permanent.

The files where the changes need to made are:

- .cshrc
- .login
- .logout

For example the command to change the settings in the .cshrc file is:

uhunix% cat .cshrc

@(#)cshrc 1.11 89/11/29 SMI
umask 077
set notify
set history = 100
set path = ( /usr/ucb /bin /usr/bin /etc …

What are restricted shells and what commands are banned in such shells?

Whenever the user runs / executes a script or a portion of it in the restricted mode it causes the shell to disable certain commands which would otherwise be available for the users. This can be considered to a security feature of unix which minimises the risk of damage while running a script. The commands or actions that are disabled during a restricted execution are as follows:

- The cd command to change the directory is restricted.
- The changing of the environment variables is not allowed.
- The user is not permitted to perform output redirection.
- To exit the restricted mode within a script is also not allowed.
- The exec method cannot be invoked to substitute a different process for the shell.
Unix - What are the Unix system calls for I/O?
What are the Unix system calls for I/O? - creat(name, permissions).......
Unix - How are devices represented in UNIX?
How are devices represented in UNIX? - UNIX represents all devices as files........
Unix - Brief about the directory representation in UNIX
Brief about the directory representation in UNIX - A UNIX directory is representing a file that consists of a correspondence between file names and inodes........
Post your comment
Discussion Board
Unix interview questions and answers
How to manage multiple connections?
You can use select() or poll() functions to manage your multiple connections. These functions somewhat has the same functionality but they are quite different in their approach. These processes examine a set of file descriptor to see specific event, if they are pending then they wait for a specific time for that event to get completed. These are useful when used for sockets, pipes,ptys,ttys and other character devices.

How can multiple processes write to the pipe simultaneously?
Multiple processes can be written to pipe simultaneously by the use of PIPE_BUF. The conditions which have to be noted down is that, if the data to be written to the pipe is less than PIPE_BUF then, the data won’t be interleaved. The read call at the same time will return all the data present, even from the multiple writes. PIPE_BUF size is at least 512KB. It is defined in “<limits.h>.

What are pttys?
Pttys are also called as Pseudo-teletypes, these are pseudo-devices that consist of two parts, which are as follows: 1st is the master side, which can be considered as “user” and 2nd is the slave side, which acts like standard tty device. They allow means to emulate the behavior of serial terminal using the control of the program. Example of this is as follows: telnet uses pseudo terminal on remote system.
Rohit Sharma 12-16-2011
Unix interview questions and answers
What's the difference between fork() and vfork()?
fork() and vfork() both are system calls, but there are some differences between both of them and these differences are as follows:
- vfork() is the lower-overhead version of fork(). fork() is the original system call.
- fork() is used to copy the entire address space of the process whereas, vfork() is used to increase the functionality of fork().

Where should I put my configuration files in UNIX?
UNIX has a separate place to keep all the configuration files at one place. There are several directories according to the UNIX version you might be using. The directories are: “/var/opt/PACKAGE”, “/usr/local/lib”, “/usr/local/etc”.
User-specific configuration files are usually hidden, this is represented by a (.) before the file name, for example “.abc”. User-specific configuration file usually resides under $HOME.

What is a zombie?
A child process is called as “zombie” when a child process gets terminated and the parent process tries to call wait() on the child process. The status of it can be seen by “ps” command, a “Z” will be written in the process status field. In this case the child process will be taking the entry in the process table. So, zombie is a situation in which the child finishes its process before its parent.
Rohit Sharma 12-16-2011
Unix interview questions and answers
How do I remove a file whose name begins with a “-”?
Filename should not be saved with a “-” in the beginning. The only possible way to remove such files is, by using the command of rm and the syntax of it is as follows:
rm ./ “-”filename (file is in the current directory)
This is the remove command which is used to remove either the file or the directory.

What to do with the file if it consists of a '/' in filename?
The file which consists of a ‘/’ in filename is treated as special case and can be created by faulty kernel code. In most of the cases '/' and '\0' are the only two characters that cannot appear in a filename. The special symbol '/' is used to separate directories and files. The commands 'find' or 'rm' will find and delete this file as they treat the symbol in normal way.

Why do I get error message, when I use "rsh host command”?
The program “rsh” stands for remote shell program. When the remote account uses the C shell, then the shell executes the command for you. Shell reads your remote “.cshrc” file which might contain other commands such as sty, biff that is not appropriate to be used with the shell. Due to this unexpected error message occurs.
For example:
stty erase ^H //commands in .cshrc file.
biff y
// when the shell interprets it, then it gives error message like
% rsh some-machine date
stty: : Can't assign requested address
Where are you?

How do I set an environment variable to change my directory inside a program?
The environment variable can’t be set as it is not allowed and special permissions are required to make any changes. Whenever a process gets created, then other processes which are the sub-process of the parent process also called as child process inherits a copy of its parent’s variable. Child process can change the values but it will have no affect on the parent shell. For allowing your parent shell to change its variables, when you change the variable in child shell, then you will be required to take permission from parent shell to give special privileges for any change.
Rohit Sharma 12-16-2011