Your Complete Solution to Shell Scripting Interviews in 2024

Last updated on Nov 03,2023 72.2K Views
Research Analyst, Tech Enthusiast, Currently working on Azure IoT & Data Science... Research Analyst, Tech Enthusiast, Currently working on Azure IoT & Data Science with previous experience in Data Analytics & Business Intelligence.

Your Complete Solution to Shell Scripting Interviews in 2024

edureka.co

Linux has started to expand its market rapidly since the past few years and Shell Scripting in Linux is one of the Top 10 occurring IT job-requirements. So, we thought of making your job easier by making an ensemble of the most commonly asked Shell Scripting Interview Questions which will get you ready for any job interview that you wish to appear.

The questions have been segregated into 3 parts;

Interview Questions for Beginners

Q1. What is Shell?

The Shell is a Command Line Interpreter. It translates commands entered by the user and converts them into a language that is understood by the Kernel. The shell interprets a command typed in at the terminal, and calls the program that you want. 

Q2. What is a Shell Script? Can you name some of its advantages? 

A shell script is a command-containing text-file that contains commands in order of their execution. Typical operations performed by shell scripts include printing text, file manipulation, and program execution.

Following are the two main advantages of shell scripting:

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

 A shell script has two types of variables :

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

There are primarily two kinds of shells in Linux OS, namely, Bourne Shell and C-Shell. Examples of derivative from each are as follows;

Q5. How do you create a shortcut in Linux?

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.

Q6. Tell something about the Super Block in Shell scripting?

A Super Block is essentially a program that contains a record of specific file systems.

Characteristics such as the block size, the empty and the filled blocks and their respective counts, the size and location of the inode tables, the disk block map, and usage information, and the size of the block groups are available in a superblock.

Q7. What is GUI scripting?

GUI is used for controlling a computer and its applications. GUI scripting supports different applications. It mostly depends on the operating system.

Q8. What are the various stages of a Linux process it passes through?

A Linux process generally passes through four stages:

Q9. What is the difference between break and continue commands?

Q10. What is the significance of the Shebang line in Shell Scripting?

The Shebang line is present at the top of the script,e.g. #!/bin/sh. It simply provides information regarding the location where the engine is placed. The engine is the one that executes the script. 

Shell Scripting Interview Questions & Answers | Edureka

This video covers the types of Linux Shell Scripting Interview Questions for candidates of beginners through an advanced level of expertise in Shell Scripting. Each segment comes with ten theory questions and 10 demos, each run on the Linux terminal for a better understanding of the viewers.

Q11. How to pass an argument to a script?

#!/bin/sh
ct $1

 

 question 11 – Shell Scripting Interview Questions – Edureka 

Q12. How to use arguments in a Script?

#!/bin/sh
cp $1 $2

 question 12 – Shell Scripting Interview Questions – Edureka 

Q13. How to calculate the number of passed arguments?

#!/bin/sh
echo "Number of Parameters passed:$#"

question 13 – Shell Scripting Interview Questions – Edureka 

Q14. How to get script name inside a script?

!/bin/sh
echo "Script Name:$0"

question 14 – Shell Scripting Interview Questions – Edureka

Q15. How to check if the previous command was run successfully?

#!/bin/sh
var=$?
if var=0
then
echo "Script was Run successfully"

else
echo "Script was unsuccessful"
fi

question 15 – Shell Scripting Interview Questions – Edureka

Q16. How to get the last line from a file using just the terminal?

tail -1 <filename>

Q17. How to get the first line from a file using just the terminal?

head -1 <filename>

Q18. How to get the 3rd element/column from each line from a file?

#!/bin/sh
awk '{print $3}' $1

question 18 – Shell Scripting Interview Questions – Edureka

Q19. How to write a function?

#!/bin/sh

function example {
echo "Hello Learner"
}

Q20. Write down the Syntax for all the loops in Shell Scripting.

For Loop:

for var in word1 word2 ... wordN
do
   Statement(s) to be executed for every word.
done

While Loop:

while command
do
   Statement(s) to be executed if command is true
done

Until Loop:

until command
do
   Statement(s) to be executed until command is true
done

Interview Questions for Intermediate

Q21. What makes C shell a more preferable option than the Bourne Shell?

C is a more preferable option in the following cases:

Q22. How would you compare the strings in a Shell Script?

The test command is used to compare the text strings. The test command compares text strings by comparing each character in each string.

Q23. How to redirect both standard output and standard error to the same location?

The two methods to redirect standard output and standard error to the same location are the following;

Q24. Differentiate between ‘ and ” quotes.

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

It is not advisable to use Shell scripting in the following cases;

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

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

Q27. What is a file system?

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

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

On Linux and other Unix-like operating systems, new files are created with a default set of permissions. The umask or user mask command is used to determine the default permissions for newly created files. It is a 4-digit Octal number which is set and expressed using symbolic values. The default permission of a file when it is created is 664 i.e. rw-rw-r-. The table for file permissions is given below;

00No permissions
11execute
22write
31+2execute + write
44read
51+4execute + read
62+4write + read
71+2+4execute + write + read

Q29. What does it mean by #!/bin/sh or #!/bin/bash at the beginning of every script?

script may specify #!/bin/bash on the first line, meaning that the script should always be run with bash, rather than another shell. /bin/sh is an executable representing the system shell. Actually, it is usually implemented as a symbolic link pointing to the executable for whichever shell is the system shell.

Q30. What is the difference between $* and $@?

$@ treats each quoted arguments as separate arguments but $* will consider the entire set of positional parameters as a single string.

Q31. Determine the output of the following command: name=Shubham && echo ‘My name is $name’.

question 31 – Shell Scripting Interview Questions – Edureka

Q32. Determine the output of the following command: [ -z “” ] && echo 0 || echo 1 

question 32 – Shell Scripting Interview Questions – Edureka

Q33. Determine the output of the following command: echo ${new:-variable}

q33 – Shell Scripting Interview Questions – Edureka

Q34. How to get part of string variable with echo command only?

#!/bin/sh
echo ${variable:x:y}
#x - start position
#y - length

variable="My name is Upasana, and I work at Edureka."
echo ${variable:11:7} # will display Upasana

q34 – Shell Scripting Interview Questions – Edureka

Q35. Rewrite the command to print the sentence and convert the variable to plural: echo “I like $variable”.

q35 – Shell Scripting Interview Questions – Edureka

Q36. How to print all the arguments provided to the script?

#!/bin/bash  for i; do     echo $i  done


q36 – Shell Scripting Interview Questions – Edureka

Q37. How to print PID of the current shell?

#!/bin/sh

for PID in $$
do
echo $PID
done

q37 – Shell Scripting Interview Questions – Edureka

Q38. How to print all array elements and their respective indexes?

!/bin/sh
array=("This" "is" "Shell" "Scripting")
echo ${array[@]}
echo ${!array[@]}

q38 – Shell Scripting Interview Questions – Edureka

Q39. How to print the first array element?

#!/bin/sh
array=("This" "is" "Shell" "Scripting" )
echo ${array[0]}

q39 – Shell Scripting Interview Questions – Edureka

Q40. What is the Crontab?

Crontab stands for cron table because it uses the job scheduler cron to execute tasks. The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. 

The schedule is called the crontab, which is also the name of the program used to edit that schedule.

Interview Questions for the Experienced

Q41. How many fields are present in a crontab file and what does each field specify?

The crontab file has six fields.

The first five fields contain information on when to execute the command and they are as follows;

The sixth field contains the command to be executed.

Q42. What are the two files of crontab command?

The two files of crontab command are:

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

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

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

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

Q45. What are the different communication commands available in the Shell?

There are four different communication commands available in Shell.

The total disk space used by Edureka can be found out as shown below.

du –s/home/Edureka

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

Given below are some common methods used to debug the problems in the script.

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

Q49. How to open a read-only file in the Shell?

A read-only file can be opened using the below command:

vi –R <File Name>

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

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

tar –tvf <File Name>.tar

Q51. Write a shell script to get current date, time, user name and current working directory.

#!/bin/sh
echo "Hello, $LOGNAME"
echo "Today's date is `date`"
echo "Username is `who i am`"
echo "Current directory is `pwd`"

q51 – Shell Scripting Interview Questions – Edureka

Q52. How to find all the files modified in less than 3 days and save the record in a text file?

find . -type f -mtime -3 -exec ls -l {} ; > last3days.txt

Q53. Write a Shell Script that adds two numbers if provided as the command Line Argument and if the two numbers are not entered throws an Error Message.

#!/bin/sh
# The Shebang

if [ $# -ne 2 ]
# If two Inputs are not received from Standard Input

then
# then execute the below statements

echo "Usage - $0 x y"
# print on standard output, how-to use the script (Usage - ./1.sh x y )

echo " Where x and y are two nos for which I will print sum"
# print on standard output, “Where x and y are two nos for which I will pri$

exit 1
# Leave shell in Error Stage and before the task was successfully carried o$

fi

# print on standard output, how-to use the script (Usage - ./1.sh x y )

echo " Where x and y are two nos for which I will print sum"
# print on standard output, “Where x and y are two nos for which I will pri$

exit 1
# Leave shell in Error Stage and before the task was successfully carried o$

fi
# End of the if Statement.

echo "Sum of $1 and $2 is `expr $1 + $2`"
# If the above condition was false and user Entered two numbers as a command$

Case 1: When parameters are not passed

q52.1 – Shell Scripting Interview Questions – Edureka

Case 2: When parameters are correctly passed

q52.2 – Shell Scripting Interview Questions – Edureka

Q54. Print a given number, in reverse order using a Shell script such that the input is provided using command Line Argument only.

#!/bin/sh
if [ $# -ne 1 ]
then
echo "Usage: $0 number"
echo " Reverse of the given number will be printed"
echo " For eg. $0 0123, 3210 will be printed"
exit 1
fi

n=$1
rev=0
sd=0

while [ $n -gt 0 ]
do
sd=`expr $n % 10`
rev=`expr $rev * 10 + $sd`
n=`expr $n / 10`
done

Case 1: When parameters are not passed

q54.1 – Shell Scripting Interview Questions – Edureka

Case 2: When the parameter is correctly passed

q54.2 – Shell Scripting Interview Questions – Edureka

Q55. Calculate a real number calculation directly from the terminal and not any shell script. 

q55 – Shell Scripting Interview Questions – Edureka

Q56. How can you get the value of pi till a 100 decimal places?

q56 – Shell Scripting Interview Questions – Edureka

Q57. How will you find the total disk space used by a specific user?

du -sh ~

q57 – Shell Scripting Interview Questions – Edureka

Q58. How to check if a directory exists?

#!/bin/sh

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

q58 – Shell Scripting Interview Questions – Edureka

Q59. Can you write a script to portray how set –x works?

#!/bin/sh
#set -x
i=1
while [ $i -lt 6 ]
do
print "in loop iteration: $i"
((i+=1))
done
exit

q59.1 – Shell Scripting Interview Questions – Edureka

#!/bin/sh
set -x
i=1
while [ $i -lt 6 ]
do
print "in loop iteration: $i"
((i+=1))
done
exit

q59.2 – Shell Scripting Interview Questions – Edureka

Q60. Suppose you execute a command using exec, what will be the status of your current process in the shell?

All the forked processes which are new get overlays when the exec is executed. The command simply gets executed without making any impact on the current process. Also, no new process will be created in this scenario.

These were all the questions we could think of, on the various aspects of Shell Scripting in Linux, that would cover you for any interview you’d appear for, in the shortest possible time frame. If you have any interesting questions that have been asked to you in an interview you’ve appeared before, feel free to drop them at the comments bar and we shall answer them for you. You could also refer to this video which explains the solutions to these problems in depth.

If you wish to learn Linux Administration and build a colorful career, then check out our Linux Administration Training which comes with instructor-led live training and real-life project experience. This training will help you understand Linux Administration in depth and help you achieve mastery over the subject.

Upcoming Batches For Linux Administration Certification Training Course
Course NameDateDetails
Linux Administration Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES