Shell Programming: Interview Questions & Answers

Knowledge | Unix/Linux Tutorials


Shell Programming Questions

This bash script tutorial assumes no previous knowledge of bash scripting. As you will soon discover in this quick comprehensive bash scripting guide, learning the bash shell scripting is very easy task. However, if you do not find an answer to your questions by reading this bash tutorial or you need extra help, feel to send us an email with the specify question. We will be more than happy to help you with your bash questions there.

Interview Questions Sets : Shell Programming

How do you find out what’s your shell?
Answer: echo $SHELL

What’s the command to find out today’s date?
Answer: date

What’s the command to find out users on the system?
Answer: who

How do you find out the current directory you’re in?
Answer: pwd

How do you remove a file?
Answer: rm

How do you remove a file?
Answer: rm -rf

How do you find out your own username?
Answer: whoami

How do you send a mail message to somebody?
Answer: mail info@comapandomain.com -s ‘Your subject’ -c ‘ support@companydomain.com‘

How do you count words, lines and characters in a file?
Answer: wc

How do you search for a string inside a given file?
Answer: grep string filename

How do you search for a string inside a directory?
Answer: grep string *

How do you search for a string in a directory with the subdirectories recursed?
Answer: grep -r string *

What are PIDs?
Answer: They are process IDs given to processes. A PID can vary from 0 to 65535.

How do you list currently running process?
Answer: ps

How do you stop a process?
Answer: kill pid

How do you find out about all running processes?
Answer: ps -ag

How do you stop all the processes, except the shell window?
Answer: kill 0

How do you fire a process in the background?
Answer: ./process-name &

How do you refer to the arguments passed to a shell script?
Answer: $1, $2 and so on. $0 is your script name.

What’s the conditional statement in shell scripting?
Answer: if {condition} then … fi

How do you do number comparison in shell scripts?
Answer: -eq, -ne, -lt, -le, -gt, -ge

How do you test for file properties in shell scripts?
Answer: -s filename tells you if the file is not empty, -f filename tells you whether the argument is a file, and not a directory, -d filename tests if the argument is a directory, and not a file, -w filename tests for writeability, -r filename tests for readability, -x filename tests for executability

How do you do Boolean logic operators in shell scripting?
Answer: ! tests for logical not, -a tests for logical and, and -o tests for logical or.

How do you find out the number of arguments passed to the shell script?
Answer: $#

What’s a way to do multilevel if-else’s in shell scripting?
Answer: if {condition} then {statement} elif {condition} {statement} fi

How do you write a for loop in shell?
Answer: for {variable name} in {list} do {statement} done

How do you write a while loop in shell?
Answer: while {condition} do {statement} done

How does a case statement look in shell scripts?
Answer: case {variable} in {possible-value-1}) {statement};; {possible-value-2}) {statement};; esac

How do you read keyboard input in shell scripts?
Answer: read {variable-name}

How do you define a function in a shell script?
Answer: function-name() { #some code here return }

How does getopts command work?
Answer: The parameters to your script can be passed as -n 15 -x 20. Inside the script, you can iterate through the getopts array as while getopts n:x option, and the variable $option contains the value of the entered option.

What’s a way to do multilevel if-else’s in shell scripting?
Answer: if then elif fi

How do you write a for loop in shell?
Answer: for in do done

How do you write a while loop in shell?
Answer: while do done

How does a case statement look in shell scripts?
Answer: case in ) ;; ) ;; esac

How do you define a function in a shell script?
Answer: function-name()

How do you find out about all running processes?
Answer: ps -ag

How do you stop a process?
Answer: kill pid

How do you remove a file?
Answer: rm

How do you remove recursively?
Answer: rm -rf

What are PIDs?
Answer: They are process IDs given to processes. A PID can vary…

How do you list currently running process?
Answer: ps

What is $$?

What is a named pipe?

What does || mean?

What does && mean?

What is a loop?

What does while do?

What is a function?

What are the different kinds of loops available in shell script?
for if while case

What does $# stand for?

$# returns the number of parameters that are passed to a shell script
$? returns the exit code of the last executed command (0 : Successful, 1 or other: Failed)

What does $? return?
Will return the status of the command which is executed lastly.
0 > Success
2 > Error

How do u open a read only file in Unix?
“vi -R filename”

What is the difference between a shell variable that is exported and the one that is not exported?

If you have a string “one two three”, Which shell command would you use to extract the strings?

How do you schedule a command to run at 4:00 every morning?

How will you list only the empty lines in a file (using grep)?
grep ^$ filename.txt

When you login to a c shell, which script would be run first? (before the terminal is ready for the user)

first /etc/.login script is run & after that
~/.login is run & then ~/.cshrc is run.

How would you get the character positions 10-20 from a text file?
cat filename.txt | cut -c 10-20

How would you print just the 25th line in a file (smallest possible script please)?
tail -n +25Â | head -1 OR
head -n 25 | tail -1

How would you replace the n character in a file with some xyz?
sed ‘s/n/xyz/g’ filename > new_filename

Grep | SED |

How you will list the ordinary files in your current directory that are not user-writable?

Use Command substitution with grep to list the names of the persons from emp.lst who were born today.

How will you remove blank lines from a file using (i) grep and (ii) sed?

Locate lines longer than 100 abd smaller than 150 characters using (i) grep and (ii) sed?

How do you delete all leading and trailing spaces in all lines of a file?

Related Articles