Shell programming or shell scripting consists of most of the features that modern programming languages have to offer. Any script, simple to complex, can be developed using Shell scripting. It is a series of UNIX commands written to accomplish a particular task using a plain text file. Tasks of day-to-day life can be automated with the help of shell scripting. Â.
If you are new to the concepts of Unix, shell, and Perl, read it here. If you know how to use shell scripting and are going to be interviewed for a job that involves it, this article with the best shell scripting interview questions will help you get ready.
Shell scripting is an essential skill for any developer working in Linux or Unix environments. As companies continue to adopt open-source technologies, expertise in shell languages like Bash and PowerShell has become highly sought after.
With shell scripting knowledge developers can easily automate repetitive tasks chain commands, manage background processes, and perform many other time-saving operations within their workflow. This makes candidates proficient in shell commands and scripting stand out during technical interviews.
To help prepare aspiring developers for upcoming interviews I’ve put together this comprehensive guide on the top 25 shell scripting interview questions. These questions test your fundamental knowledge of shell scripting concepts as well as their real-world applications.
Let’s get started!
Basic Shell Scripting Interview Questions
Here are some common shell scripting interview questions aimed at beginners:
1. What is the difference between a shell variable and an environment variable?
A shell variable is only used by the current shell and any child processes that are running in the background. After it is exported, an environment variable can be used by all child processes that are started from that shell.
2. How do you set and reference the positional parameters $1
, $2
, etc. in a shell script?
The positional parameters $1
, $2
, $3
and so on allow you to access passed command line arguments by their position. $1
accesses the first argument, $2
the second, and so on. $@
allows you to access all arguments at once.
3. What is the purpose of the shebang line in Bash scripts?
The shebang line which starts with #!
specifies the interpreter to execute the rest of the script. For example, #!/bin/bash
indicates that the Bash shell interpreter should be used.
4. How can you get a list of all shell variables set in the current shell?
The set
built-in command will display all variables when invoked without any options or arguments.
5. What is a PID and how would you get the PID of a running process in Linux?
PID or process ID uniquely identifies a running process on Linux. To find the PID of a process, use the pidof
command passing in the process name. You can also use ps aux | grep process_name
and find the PID from the output.
6. What are the different ways to run a shell script on Linux?
Some ways to execute a shell script on Linux include:
- Specify path
./script.sh
- Include path in shebang
#!/bin/bash/script.sh
- Run as
bash script.sh
- Make the script executable with
chmod +x
7. How can you accept user input in a shell script?
The read
built-in command allows you to accept user input in a shell script. For example, read -p "Enter your name: " name
will prompt the user to enter their name which gets stored in the name
variable.
8. What are the different wildcard characters available in Bash?
The most common wildcard characters used in Bash are:
*
matches any string of characters?
matches any single character[]
matches any character within the bracket[!]
matches any character not in the bracket
9. What is command substitution and how is it used?
Command substitution allows you to assign the output of a command to a variable or pass it as an argument to another command. It is denoted by $(command)
syntax. For example, today=$(date)
will set today
to the output of the date
command.
Intermediate Shell Scripting Interview Questions
Let’s move on to some more challenging shell scripting questions:
10. How can you debug a bash script? What options can you use to print debug statements?
To debug bash scripts, use the set -x
option which will print commands before executing them. You can also use set -v
to echo commands as they are read. Debug messages can be printed using echo
.
11. What is the difference between &>
and 2>&1
for file redirection in bash?
2>&1
redirects stderr to stdout. &>
redirects both stdout and stderr to the file. The order is important: &>file
will overwrite file whereas >file 2>&1
will append.
12. How do you catch errors and exceptions in bash scripts?
The trap
command can be used to handle signals and catch errors in bash scripts. For example, trap "echo Error" ERR
will execute the echo statement whenever a command in the script exits with non-zero status.
13. What is the difference between a hard link and a symbolic link?
A hard link points directly to the physical file on disk whereas a symbolic link is a pointer to the original file. If the original file is deleted, the hard link will still have access whereas the symbolic link will be broken.
14. How can you iterate through the lines of a file in bash?
To read a file line-by-line, use a while
loop with the read
command:
while read line; do echo "$line"done < "file.txt"
This will print each line in the loop.
15. What are the three standard streams in Linux and how are they referenced?
The three standard streams are input stream (stdin), output stream (stdout), and error stream (stderr). They can be referenced using file descriptors 0, 1 and 2 respectively.
16. How can you run processes in the background in bash? How would you bring a background process to the foreground?
Append &
to the command to run it in the background. For example, sleep 100 &
. Use the fg
command to bring the job to foreground, referencing its job number from the jobs
command.
17. What is the difference between executables
vs .sh
scripts?
Executables are binary files compiled from source code and can be run directly. .sh
scripts contain commands in text format and require an interpreter (shell) to execute line-by-line.
18. When would you use the source
command instead of .
or bash
to execute a script?
source
executes the script within the current shell context, loading variables, functions, aliases etc. into your current session. .
and bash
spawn a subshell to run the script, preventing permanent changes to your shell environment.
19. How can you pass an entire array or list as arguments to a command in bash?
Use the @
or *
syntax:
files=(f1.txt f2.txt f3.txt) cat "${files[@]}"
This expands the files array and passes its elements as arguments to cat.
20. What is Process Substitution in bash and how is it useful?
Process Substitution allows you to redirect the input or output of a process as a file for another process. For example, diff <(ls foo) <(ls bar)
passes the outputs of the two ls
commands as files to diff
so their contents can be compared.
Advanced Shell Scripting Interview Questions
Finally, here are some advanced level shell scripting interview questions:
21. How can you optimize performance of bash scripts?
Some ways to optimize bash script performance include:
- Use more efficient commands like
[[
instead oftest
- Avoid unnecessary commands like
cat file
– use< file
instead - Parallelize tasks with
&
background operator - Initialize variables at top rather than re-declaring inside loop
- Use builtin commands over external commands like
grep
,sed
, etc.
22. What is the significance of the set
command options -e
, -o pipefail
, -u
and -f
in bash scripts?
-e
: Exit immediately if any command fails-o pipefail
: Return exit status of the last command in a pipeline-u
: Treat unset variables as errors-f
: Disable pathname expansion
23. How can you write bash scripts that are portable across different Linux distributions?
Some tips for writing portable bash scripts:
- Use standard/POSIX constructs like
$( )
instead of backticks - Avoid reliance on external tools like
sed
,grep
, etc. - Use relative paths over hardcoded absolute paths
- Stick to widely available tools like
awk
,printf
,date
etc. - Test scripts across different platforms like CentOS, Ubuntu, etc.
24. What are the differences between test, [ and [[ in bash? When should you use each?
test
is external utility,[
and[[
are bash built-ins.[
implements basic conditional testing, while[[
adds handy features like regex matching, &&, || operators etc.- Prefer
[[
over[
for portability and added features. Use `
How will you debug problems encountered in the shell program?
Some standard methods of debugging the problems in the script are:
- use of set-x to enable debugging
- You can add debug statements to a shell script to show information that helps you figure out what’s wrong. Â .
Define Shell Variable.
Shell variable forms the core part of a shell script or program. The variable allows the shell to manipulate the stored information within a shell program. It is generally stored as a string variable. Â.
SHELL INTERVIEW QUESTIONS AND ANSWERS (How to Pass a Shell Job Interview!)
FAQ
What is a Stage 2 interview?
What is an interview 2?
How do I prepare for a Level 2 interview?
What is the second interview question?
What are some shell scripting interview questions?
Here are some shell scripting interview questions for the experienced candidates! 1. Name two files of the crontab command. The two crontab command files are: 2. How will you debug problems encountered in the shell program? Some standard methods of debugging the problems in the script are:
What are some intermediate level shell scripting interview questions?
Here are some intermediate level shell scripting interview questions! 1. Tell us about the disadvantages of shell scripting. Shell scripting has the following disadvantages: Weak designs can prove to be costly and can destroy the whole process. 2. What do you understand by GUI scripting?
What is the interview process like at Shell?
Interview process is good.there will be 3 rounds .2 technical and one general. SAP SD all standard configs, business scenarios will be asked in detail.based on your interest areas there will be more questions.And interview was simple not much complicated questions. I applied online. I interviewed at Shell (Manila, Manila)
Why does a shell interviewer ask a question?
Why the interviewer is asking this: The interviewer wants to assess your basic knowledge of shells. A shell is a program or interface that accepts and translates user commands so the operating system can perform those commands or go between the user and the kernel.