Knowledge of shell scripting is required in many technical jobs, including roles in DevOps, network engineering, cybersecurity, software engineering, and systems administration. And when you’re applying for a job that’ll use shell scripting, you could very well be asked shell scripting questions during your technical interviews. One of the best ways to prepare for these questions is to practice answering them in a mock interview.
You can ask a friend or family member to play the role of the interviewer and ask you questions that might come up during your interview. This allows you to practice answering questions out loud in front of another person. If you’re interviewing remotely, consider doing your mock interview remotely as well. This way you’ll practice in an environment that’s similar to how your real interview will be conducted. And you can always practice answering questions out loud on your own if someone isn’t available for your mock interview.
To help you prepare, here are 15 shell scripting interview questions you might be asked in a technical interview.
1. What is a shell?
A shell is a command-line interpreter between either a user or an application and the operating system’s kernel. The kernel manages and controls access to the computer’s resources and hardware. The shell is a layer on top of the kernel that communicates with the kernel to send commands to the hardware.
2. What is a shell script?
A shell script is simply a series of shell commands in a plain text file executed sequentially to accomplish a task. Shell scripts allow users to store these commands so they can be run again.
3. What are some popular types of shells?
- Bourne Shell (sh): the original UNIX shell and the default for Solaris OS.
- C Shell (csh): a shell program that uses C syntax.
- Korn Shell (ksh): a Unix-based, high-level shell program.
- Bourne Again Shell (bash): an open-source shell program found on all Linux-based systems.
4. What is a shebang in a shell script?
A shebang is a hash sign (#) followed by an exclamation point (!) and a string that represents the location of the program that will execute it. For example, the following shebang means the script should be run with Bourne Again Shell:
By adding a shebang, you can execute a shell script by calling the file without adding the location of the shell to the command.
5. How do you pass and access arguments in a shell script?
You can pass arguments to a shell script to the script by listing them after the name of the script, like this:
./my_script.sh arg1 arg2
Then you can access them in the script with a dollar sign and a number that indicates the argument’s location, like this:
$1 # =arg1 $2 # =arg2
6. What does
$# do in a shell script?
This is a global variable in a shell script that gives you the count of the arguments passed to it.
7. How do you print to the terminal in a shell script?
You can print to the command line using echo.
echo "this will print"
You can also use tput, which gives you options to change the color of the text and add other text effects to the output.
8. How do you define a variable in a shell script?
To define a variable in a shell script, you use the equal sign, and to access the variable, you add a dollar sign to the name you gave the variable. It looks like this:
var = "my_variable" echo $var # prints my_variable
9. How do you print the output of a shell script to a file?
You use a greater than sign (>) and the output file in your command after the script file name, like this:
./myscript.sh > outputfile.txt
10. What command will display the contents of a file?
The cat command will output the contents of a file. The following command reads one file and then uses the greater than sign to print the results to another file.
cat text1.txt > text2.txt
11. What does a
dot (.) mean at the beginning of a file, and how would you list that file?
The dot indicates that the file is hidden. Using the standard ls command to list the files in a directory won’t list hidden files. To show them in the list, you have to add the -a option to the command, like this:
ls -a ./mydirectory
12. What types of loops can you use in a shell script?
The three types of loops you can use in a shell script are For, While, and Until. The For Loop loops through a list of items. The While Loop executes a loop while a condition returns true and exits once it’s false. The Until Loop executes a loop while a condition is false and exits once it is true.
13. What is a break statement used for?
A break statement is used to exit a loop. It’s usually used with an if statement to check a condition when the loop should be exited early.
14. How do you determine which shell you’re currently using?
You can do this by echoing the
$SHELL variable, with this command:
echo $SHELL # prints /bin/bash
15. How do you determine which types of shells you have available on your system?
You can do this with the following command:
This will output a list of shells like the following:
/bin/sh /bin/bash /bin/csh /bin/ksh
More shell scripting interview help
If you need a refresher, our beginner-friendly Learn the Command Line course covers the ins and outs of using the text-based interface of your computer so you can use it with increased speed and efficiency. If you already know the command line well, then our Learn Bash Scripting course will teach you shell scripting using Bourne Again Shell, one of the most popular versions of shell.
For more interview prep, read our guide on how to ace your technical interview, advice for the whiteboard interview, and tips on answering behavioral interview questions. If you are doing a virtual interview, check out our Zoom interview tips.
Our Career Center offers more job-hunting resources, from resume-writing tips to networking advice. And if you’re interested in learning a new skill, you can check out all the courses we offer in our course catalog.