20 Shell Scripting Questions for Practice

Shell scripting provides an easy and powerful programming method to help you save time by automating a lot of your repeated manual tasks. It’s a concept which can be vital for automation testers and DevOps engineers in interview preparations. So we came up with twenty frequently asked Shell scripting questions that you should prepare to answer during interviews.

Being a Shell script programmer, you should be aware of the different shells available on the Linux OS. And before even writing or executing a shell script, you must make sure that you are using the right Shell where the script is intended to run.

The script targetted for a particular Shell won’t run in a different Shell without making appropriate changes. So keeping a tab on the Shell environment will help you avoid any hassle that could have appeared at runtime.

In the below set of Shell scripting questions and answers, we’ve mostly used the BOURNE again shell or famously known as BASH. Also, if you are serious about preparing for a Linux interview, then do read the below posts to give full throttle to your preparations.

Now, you can continue reading the essential Shell scripting questions and examples given below.

Shell Scripting Questions and Answers for Practice.

Shell Scripting Questions and Answers for Practice.

Q-1. Presumably, what could be the reason that the <cat filename> command start giving the following error message?


The command not found error occurs when either the PATH variable is corrupt or have some issue in configuration.

Hence, it’ll throw the error message as mentioned in the question.


Q-2. How can we pass arguments to a script in Linux? And how to access these arguments from within the script?


We can write a bash script that can accept arguments from the command line in the following manner.

To access these arguments, we can use the positional parameters ($1 to $9) in the script. Each parameter corresponds to the position of the argument on the command line.

The first argument is read by the shell into the parameter $1, the second argument into $2, and so on. After $9, start enclosing the arguments in brackets like ${10}, ${11}, and ${12}.

Some shells don’t support the above method. In that case, to access parameters with numbers greater than 9, you can use the shift command. It moves the parameter list to the left.

$1 is lost, $2 becomes $1, $3 becomes $2, and so on. The inaccessible 10th parameter becomes $9 and becomes accessible.

Let’s check out an example for clarity.

Upon execution, the test script will yield the following output.


Q-3. What is the difference between $* and $@?


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


Q-4. What is the command to display the list of files in a directory?


The following command displays the list of files in a directory.


Q-5. Write a shell script to display the last updated file or the newest file in a directory?


Following is the test script to list the most recently changed file.


Q-6. Write a shell script to get the current date, time, username and current working directory.


Let’s create a file named <stats.sh> and add the following code in it.

First of all, assign the execute permission for the script and then execute it. It’ll display the required information.


Q-7. Write a shell script that adds an extension “.new” to all the files in a directory.


Please use the following script to change all the files in a directory to a “.new” extension. Kindly make sure to supply the directory name as an argument while running the test script.


Q-8. Write a shell script to print a number in reverse order. It should support the following requirements.

  • The script should accept the input from the command line.
  • If you don’t input any data, then display an error message to execute the script correctly.


Let’s first, jot down the steps involved in the test script.

1. Suppose the input number is n.
2. Set reverse and single digit to 0 (i.e. rev=0, sd=0).
3. The expression (n % 10) will give the single left most digit i.e. sd.
4. To reverse the number, use this expression <rev * 10 + sd>.
5. Decrease the input number (n) by 1.
6. If n is greater than 0, then go to step no. 3. Else, execute the step no. 7.
7. Print the result.

The script code is as follows.

There are two ways in which we can run the above script.

Instance 1:

If there is no input, you will get the following output.

Instance 2:

When the input is available in the command line Argument, you will get the following output.


Q-9. How will you delete a file which has special characters in its file name?


In Linux or Unix-like system, you may come across file names including the following special characters, white spaces, backslashes and more.

Bash shell considers most of the above special characters as commands. Thus, the “rm” command may not be able to delete such files. The simplest way to delete files having special characters in its name is by using the inode number.


The <-i> option of <ls> command displays the index number (inode) of each file.


Use the <find> command given below to delete the file, if it has inode number such as 9966571.


Q-10. How to ask for input in a shell script from the terminal?


In Linux, we can use the <read> command to take input from the user. It reads data from the terminal as the user enters it from the keyboard. And stores into a variable.

Let’s see a sample test script featuring the <read> command.

Upon running the above script, it’ll prompt for the name and assigns the input value to the variable <name>.


Q-11. How can we perform numeric comparisons in Linux?


The test command can perform various types of numeric comparison using the following operators.

1. <eq>

Description: INTEGER1 is equal to INTEGER2
Example Code:

2. <ge>

Description: INTEGER1 is greater than or equal to INTEGER2
Example Code:

3. <gt>

Description: INTEGER1 is greater than INTEGER2
Example Code:

4. <le>

Description: INTEGER1 is less than or equal to INTEGER2
Example Code:

5. <lt>

Description: INTEGER1 is less than INTEGER2
Example Code:   

6. <ne>

Description: INTEGER1 is not equal to INTEGER2
Example Code:


Q-12. What is the syntax for different types of loops available in shell scripting?


Following are the loops supported in shell scripting.

1. <for loop>.

2. <while loop>.

3. <until loop>.

4. <select loop>.


Q-13. How will you find the sum of all numbers in a file in Linux?


Following is the script which computes the sum of all numbers stored in a file.


Q-14. Write a shell script to delete the lines containing a word <dd> if it appears between the 5th and 7th position?


Let’s take a fixed width file as:

The expected output after script execution is.

The test script to do the expected task is as follows.


Q-15. Write a shell script to find out the unique words in a file and also count the occurrence of each of these words. We can say that file under consideration contains many lines, and each line has multiple words.


Following test script/command will count the unique words.


Q-16. Write a shell script to get the total count of the word “Linux” in all the “.txt” files and also across files present in subdirectories. 


The following is the test script/command which recursively searches for the “.txt” files and returns the total occurrences of a word <Linux>.


Q-17. Write a shell script to validate password strength. Here are a few assumptions for the password string.

  • Length  – minimum of 8 characters.
  • Contain both alphabet and number.
  • Include both the small and capital case letters.

If the password doesn’t comply to any of the above conditions, then the script should report it as a <Weak Password>.



Q-18. Write a shell script to print the count of files and subdirectories in the specified directory.



Q-19. Write a shell script to print the reverse of an input number.



Q-20. Write a shell script to reverse the list of strings and reverse each string further in the list.



Summary – Essential Shell Scripting Questions and Answers.

We are hopeful that the above essential Shell scripting questions and answers would help you immensely. If you’ve any questions, then please feel free to let us know about it.

Listening to your feedback makes us do better so share it.

You can even ask us to write on a topic of your choice. We’ll add it to our writing roadmap.

Lastly, if you’d enjoyed the post, then please care to share it with friends and on social media.

Keep Learning,


Leave a Reply