Real shell scripting interview questions

Q What is Shell?

Ans: 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 operating system.

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

Ans: Two options
1) Execute the script as “sh -x”
2) Put “set -x” in the script

Q Which is the symbol used for comments in bash shell scripting ?


Q What is the difference between = and ==?


=      -> It is used for assigning value to the variable.

==    -> It is used for string comparison.

Q How to get 4th element from each line of a file ?


awk '{print $4}'

Q What needs to be done before you can execute a shell script?

You need to make the shell script executable using the chmod command.

This chmod command makes the shell script file “file1” executable for the user (owner) only:
$ chmod u+x file1

Below syntax makes it executable for all (everyone):

$ chmod a+x file1

Q How to pass argument to a script ?

./script argument

Q How do you terminate an if statement?

Ans: Using “fi” . Check example below.

Q Give an example of if else statement

End the year strong with courses as low as $11.99 script to test if the file exists
if [ -f $cvfile ]
echo "$cvfile exists"
"$cvfile does not exists"
exit 0

So you will execute the script as “./ file1” .


Q How to check if a directory exists?


if [ -d $mydir ] 
echo "Directory exists"


Q How to calculate number of passed arguments ?



Q How to check if previous command run successful ?


echo $?

If exit code is 0 it means command ran successfully

Q How to get last line from a file ?


tail -1

Q How to redirect stdout and stderr streams to log.txt file from inside the script ?

Add “exec >log.txt 2>&1” put this as the first command in the script

Q How to remove blank lines from a file?

Ans :

grep -v '^$' testcv.txt > testcv2.txt

Q Write a command to find all the files modified in less than 3 days and print the record count of each?

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

Q How to find a process name from process ID?

ps -p PID

Leave a Reply

Your email address will not be published. Required fields are marked *