70 Shell Scripting Interview Questions & Answers
70 Shell Scripting Interview Questions & Answers
LINUX HOWTO
LINUX DISTROS
SHELLLINUX
SCRIPTS
COMMANDS
70 LINUX
Shell Scripting Interview Questions & Answers
TOOLS
AUTOMATION
Posted on : May 18, 2015 , Last Updated on : October 8, 2016 By Petras Liumparas More
SHELL SCRIPTS
We have selected
DEVOPS expected 70 shell scripting question and answers for your interview preparation. Its really vital for all system admin to know
scripting
WEB or SERVERS
atleast the basics which in turn helps to automate many tasks in your work environment. In the past few years we have seen that all
linux job specification
UBUNTU HOWTO requires scripting skills.
./script argument
./show.sh file1.txt
cat show.sh
#!/bin/bash
cat $1
cat copy.sh
#!/bin/bash
cp $1 $2
$#
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 1/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
$0
$?
tail -1
head -1
9) How to get 2nd element from each line from a file, if first equal FIND
Example
#!/bin/bash –xv
function example {
echo "Hello world!"
}
V1="Hello"
V2="World"
let V3=$V1+$V2
echo $V3
Output
Hello+World
V1=1
V2=2
V3=$V1+$V2
echo $V3
Output
3
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 2/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
if [ -f /var/log/messages ]
then
echo "File exists"
fi
for loop :
for i in $( ls ); do
echo item: $i
done
while loop :
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
until loop :
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
That line tells which shell to use. #!/bin/bash script to execute using /bin/bash. In case of python script there there will be #!/usr/bin/python
19) What would be the output of command: [ -z "" ] && echo 0 || echo 1
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 3/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
26) What would be the output of the command: name=John && echo 'My name is $name'
My name is $name
27) Which is the symbol used for comments in bash shell scripting ?
variable
30) How to redirect stdout and stderr streams to log.txt file from script inside ?
31) How to get part of string variable with echo command only ?
echo ${variable:x:y}
x - start position
y - length
example:
variable="My name is Petras, and I am developer."
echo ${variable:11:6} # will display Petras
32) How to get home_dir with echo command only if string variable="User:123:321:/home/dir" is given ?
echo ${variable#*:*:*:}
or
echo ${variable##*:}
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 4/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
echo ${variable%:*:*:*}
or
echo ${variable%%:*}
34) How to list users which UID less that 100 (awk) ?
35) Write the program which counts unique primary groups for users and displays count and group name only
IFS=":"
${#variable}
echo ${variable//pattern/replacement}
tr '[:lower:]' '[:upper:]'
set ${string}
echo $#
export variable
ls -d ?[ab]*
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 5/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
c=$((a+b))
or
c=`expr $a + $b`
or
c=`echo "$a+$b"|bc`
48) Rewrite the command to print the sentence and converting variable to plural: item="car"; echo "I like $item" ?
49) Write the command which will print numbers from 0 to 100 and display every third (0 3 6 9 …) ?
echo $*
or
echo $@
[ $a -gt 12 ]
[ $b -le 12 ]
[[ $string == abc* ]]
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 6/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
echo $$
echo $#
echo ${array[0]}
echo ${array[@]}
echo ${!array[@]}
unset array[2]
array[333]="New_element"
a) via parameters
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 7/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
Good luck !! Please comment below if you have any new query or need answers for your questions. Let us know how well this helped for your
interview :-)
PREVIOUS ARTICLE
NEXT ARTICLE
Comments (13)
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 8/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
1. vicdeveloper says:
May 27, 2015 at 4:01 am
Great!.
This article is a gem. Oh my god, a lot of useful info about shell scripting.
Thanks :D!
Reply
2. Ashish says:
March 12, 2016 at 10:37 am
Reply
3. Abhishek says:
May 29, 2016 at 1:59 pm
Really awesome?....
Reply
4. Harish says:
June 5, 2016 at 9:31 am
Reply
Reply
6. anumolu says:
August 21, 2016 at 10:48 pm
It's nice..!!
Reply
Hi Petras Liumparas,
This is very very useful information on shell scripting, Please comeup with advanced tutorial like this.
I hope you will keep posting this kind of tutorial in shell script, especially in advanced level. if possible cover SED in detail.
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 9/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
Thanks a lot.
Reply
8. BhanuChand says:
September 5, 2016 at 6:34 pm
Great collection.
Reply
9. deepak says:
November 15, 2016 at 11:30 am
good one
Reply
Great Collection. Also it would be great if you can share common shell script errors and error handling related stuff.
Thanks in Advance.
Reply
Reply
Reply
Reply
Leave a Reply
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 10/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
Name( required )
Website
Submit Comment
Popular Posts
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 11/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
Sunway
College JB
Sunway College JB
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 12/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
Submit Your
Resume Now
Ad Monster.com Malaysia
Shell Script to
Check Linux...
linoxide.com
Ad moderncancerhospital.com.my
Shell Script to
Backup Files /...
linoxide.com
Amazing ! 25 Linux
Performance...
linoxide.com
30 Linux TOP
Command Usage...
linoxide.com
Learning Shell
Scripting - First...
linoxide.com
How to Show
Dialog Box from...
linoxide.com
26 Linux Interview
Questions and...
linoxide.com
15 Advanced Linux
Interview...
linoxide.com
Linux 'make'
Command...
linoxide.com
Linux Commands
Cheat Sheet in...
linoxide.com
Linux Boot
Process -...
linoxide.com
LINUX COMMANDS
27 APR, 2017
24 MAR, 2017
13 MAR, 2017
19 SEP, 2016
How to Manage Time from Linux Terminal
using Ding Tool
Unix Z Commands – Zcat, Zless, Zgrep, Zegrep
and
31 Zdi2017
MAY, Examples
7 SEP, 2016
How to Install OpenMaint on Ubuntu 16.04
How
29 to2017
MAY, Track Progress % of Commands
(cp,dd,tar) in CentOS 6 / Fedora
Machma
5 SEP, 2016- Enables to Run Multiple Commands
Parallel in Linux
Run
24 Command
MAY, 2017 Parallel on Multiple Hosts
using PDSH Tool
How to2016
30 AUG, Setup Elastix 5 PBX Uni ed
Communication Server on Linux SUBSCRIBE TO FREE NEWSLETTER
Powerful
22 SSH
MAY, 2017 Command Options with
Examples on Linux Get Delivered Free In Your Inbox Something New About Linux
26 JUL, 2016
Join 10400+ Linux Troop
watch - Repeat Linux / Unix Commands
Regular Intervals
Enter Your Email * SUBSCRIBE
4 JUN, 2015
100% privacy - We will never spam you
A Peep into Process Management Commands
in Linux
TAGS
25 MAR, 2015
Apache Backup Clustering Command Container Docker Filesystem Firewall Freebsd Linux Tools Monitoring Network Ssh Storage Ubuntu 16
How to Manage Network using nmcli Tool in
RedHat / CentOS 7.x
14 JAN, 2015
5 JAN, 2015
28 NOV, 2014
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 14/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
A Great Tool To Show Linux Command
Progress Like % , ETA
24 NOV, 2014
21 NOV, 2014
19 NOV, 2014
5 NOV, 2014
27 OCT, 2014
6 OCT, 2014
1 OCT, 2014
5 SEP, 2014
29 AUG, 2014
18 AUG, 2014
16 AUG, 2014
15 AUG, 2014
13 AUG, 2014
23 JUL, 2014
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 15/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
Linux slabtop command - Display Kernel Slab
Cache Information
11 JUL, 2014
7 JUL, 2014
23 JUN, 2014
16 JUN, 2014
30 MAY, 2014
2 MAY, 2014
28 APR, 2014
28 MAR, 2014
21 MAR, 2014
14 MAR, 2014
2 MAR, 2014
26 FEB, 2014
24 FEB, 2014
19 FEB, 2014
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 16/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
14 FEB, 2014
10 FEB, 2014
5 FEB, 2014
3 FEB, 2014
27 JAN, 2014
23 JAN, 2014
14 JAN, 2014
10 JAN, 2014
9 JAN, 2014
1 JAN, 2014
28 DEC, 2013
26 DEC, 2013
22 DEC, 2013
21 DEC, 2013
20 DEC, 2013
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 17/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
17 DEC, 2013
14 DEC, 2013
13 DEC, 2013
12 DEC, 2013
10 DEC, 2013
10 DEC, 2013
10 DEC, 2013
8 DEC, 2013
7 DEC, 2013
6 DEC, 2013
3 DEC, 2013
1 DEC, 2013
28 NOV, 2013
24 NOV, 2013
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 18/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
20 Linux Cat Command Examples For File
Management
23 NOV, 2013
20 NOV, 2013
17 NOV, 2013
6 NOV, 2013
30 SEP, 2013
11 SEP, 2013
5 SEP, 2013
2 SEP, 2013
26 AUG, 2013
19 AUG, 2013
12 AUG, 2013
5 AUG, 2013
29 JUL, 2013
22 JUL, 2013
8 JUL, 2013
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 19/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
7 Linux Tail Command Examples and How it
Helps Monitor Logs
1 JUL, 2013
24 JUN, 2013
20 JUN, 2013
29 MAY, 2013
5 MAY, 2013
17 APR, 2013
15 APR, 2013
11 APR, 2013
3 APR, 2013
18 MAR, 2013
14 MAR, 2013
1 FEB, 2013
30 JAN, 2013
28 JAN, 2013
26 JAN, 2013
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 20/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
Commands To Understand Page Faults Stats In
Linux
10 JAN, 2013
29 DEC, 2012
11 DEC, 2012
8 DEC, 2012
5 DEC, 2012
30 NOV, 2012
28 NOV, 2012
25 NOV, 2012
23 NOV, 2012
21 NOV, 2012
10 NOV, 2012
3 NOV, 2012
2 NOV, 2012
20 OCT, 2012
18 OCT, 2012
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 21/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
2 OCT, 2012
12 SEP, 2012
1 SEP, 2012
6 MAY, 2012
20 OCT, 2011
7 SEP, 2011
7 SEP, 2011
6 SEP, 2011
6 SEP, 2011
6 SEP, 2011
22 AUG, 2011
22 AUG, 2011
24 JUN, 2011
21 JUN, 2011
27 MAY, 2011
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 22/24
7/1/2017 70 Shell Scripting Interview Questions & Answers
20 Examples for Linux Zip and UnZip
Command
18 MAY, 2011
16 MAY, 2011
13 MAY, 2011
10 MAY, 2011
2 MAY, 2011
30 APR, 2011
30 APR, 2011
30 APR, 2011
24 APR, 2011
24 APR, 2011
23 APR, 2011
23 APR, 2011
23 APR, 2011
23 APR, 2011
20 MAR, 2011
20 MAR, 2011
20 MAR, 2011
25 FEB, 2011
25 FEB, 2011
22 FEB, 2011
https://linoxide.com/linux-shell-script/shell-scripting-interview-questions-answers/ 24/24