Practical Interview Questions and Answers on Linux Shell Scripting

With the overwhelming response we are getting on the Interview Series articles, the first of its kind on any Linux How-to website in the form of Likes, Feedbacks in comment as well as on personal Email Address make us going from one article to the next article.

Here is the link to the Interview Series articles already published on, where we have covered lots of topics viz., FTP, MySQL, Apache, Scripting, Linux Commands, etc.

Practical Interview Question on Shell  Scripting
Practical Interview Question on Shell Scripting

Continuing to the above series here we are coming with another 5 wonderful Linux Interview Questions and their answer. Your (The Readers and frequent Visitors) support is always needed to make it successful.

1. Write a shell script to get current date, time, user name and current working directory.
Answer : The commands logname, date, who I am and pwd will output username, current date and time, and current working directory. Just implementing these commands in the script and making it a bit interactive.

Now create a file called ‘‘ and add the following code to it.

echo "Hello, $LOGNAME" 
echo "Current date is `date`" 
echo "User is `who i am`" 
echo "Current directory `pwd`"

Place execute permission and run the script as shown below.

# chmod 755 
# ./
Sample Output
Hello, avi 
Current date is Sat Jun  7 13:05:29 IST 2014 
User is avi      pts/0        2014-06-07 11:59 (:0) 
Current directory /home/avi/Desktop
2. Write a Shell Script that adds two numbers if provided as the command Line Argument and if the two numbers are not entered it outputs an Error Message along with a one-Line of how-to use description.
Answer : Here is the simple Shell Script along with description which adds two number if provided as Command-Line argument, if not it throws error with single line on how to use the script.

Again create a file called ‘‘ and add the following content to it.

# The Shebang

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

# then execute the below statements

    echo "Usage - $0   x    y" 
    # print on standard output, how-to use the script (Usage - ./   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 print sum ”

    exit 1 
    # Leave shell in Error Stage and before the task was successfully carried out.

# 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 Line Argument,   
       it will show the sum of the entered numbers.

Set executer permission on the file and run the script as shown below.

# chmod 755

Condition 1: Running the script without entering two numbers as command line argument, you will get the following output.

Sample Output
# ./

Usage - ./   x    y 
        Where x and y are two nos for which I will print sum

Condition 2: When Numbers are entered as command line argument you will get the result as shown.

$ ./ 4 5 

Sum of 4 and 5 is 9

Hence the above shell script fulfills the condition as suggested in the question.

3. You needs to print a given Number say 10572, in reverse order using a Shell script such that the input is provided using command Line Argument only. If the input data is not provided as Command Line Argument, it should throw and error and should suggest, how to use the script. Write the script but before that tell me the algorithm that needs to be implemented here.


  1. 1. Let the Input Number = n
  2. 2. Set rev=0, sd=0 (Reverse and single digitis set to 0)
  3. 3. n % 10, will find and give single left most digit
  4. 4. reverse number is generated as rev * 10 + sd
  5. 5. Decrease Input Number (n) by 1.
  6. 6. if n > 0, then goto step 3 else goto setp 7
  7. 7. Print rev

Now again, create a file called ‘‘ and add the following given code it.

if [ $# -ne 1 ] 
    echo "Usage: $0   number" 
    echo "       I will find reverse of given number" 
    echo "       For eg. $0 0123, I will print 3210" 
    exit 1 


while [ $n -gt 0 ] 
    sd=`expr $n % 10` 
    rev=`expr $rev \* 10  + $sd` 
    n=`expr $n / 10` 
    echo  "Reverse number is $rev"

Grant a execute permission on the file and run the script as shown below.

# chmod 755 numbers.h

Condition 1: When Input is not provided as command line argument, you will get the following output.

Sample Output

Usage: ./  number 
       I will find reverse of given number 
       For eg. ./ 123, I will print 321

Condition 2: When Input was provided as command line Argument.

$ ./ 10572 

Reverse number is 27501

The above script worked perfectly and the output was just what we needed.

4. You are supposed to calculate a real number calculation directly from terminal and not any shell script. What will you do (say the real numbers are 7.56 and 2.453)?
Answer : We need to use bc command in a special way as described below. The output of echo 7.56 + 2.453 should be pipelined to bc.

For example, run the following command to calculate numbers in real time using bc command as shown.

$ echo 7.56 + 2.453 | bc

5. You are supposed to find the value of pi upto 100 places of decimal, what is the easiest way to get the result done.
Answer : The easiest way to find the value of pi, correct upto 100 places of decimal we just need to issue the below command.
# pi 100 


Obviously! We must have package ‘pi‘ installed. Just do a apt or yum to get a required package to install ‘pi‘ onto the distribution you are using.

That’s all for now. I’ll be here again with another interesting article very soon. Till then stay tuned and connected to Don’t forget to provide us with you valuable feedback in the comment section below.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

A Passionate GNU/Linux Enthusiast and Software Developer with over a decade in the field of Linux and Open Source technologies.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.


Leave a Reply
  1. Hello, I’m not sure if this is the right section to post my question.

    I have a remote Ubuntu server with Xfce running on it. Here an application I would like to run via local script (remote). Any help with your great articles is appreciated, thank you in advance.

  2. Question #5 (display PI to 100 places) The “pi” command is an obscure command on Unix/Linux, that 75% of root sysadmin professionals don’t have occasion to use with 10yrs of real-job work.

    Shell scripts for business are more about server control, admin, performance tuning, storage management, databases, networking, security, monitoring, clusters, install/upgrade, reporting, and user-id setup… so they seldom need value of “pi”, let alone to 100 digits.

    The “pi” command is not even present for many Unix versions such as HP-UX, IBM’s AIX, Solaris, etc… which means you generally would not use that command in a shell script that can be used with MANY kinds of servers.

    Alternately, the “bc” command line calculator IS present on almost every form of Unix, and can also be used to calculate the value of “pi” like this:

    $  echo "scale=100 a(1)*4" | bc -l              ## Note: function a(x) is "arc-tangent(x)

    SO: While this is a valid question/answer for Linux, it may not be an accurate assessment of someone’s real-world experience on the job.

    • @Joseph

      Your analysis is on point here, however, this command : $ echo “scale=100 a(1)*4” | bc -l actually has a syntax error. It is missing a minor but important ; sign after the scale value, the correct syntax as per Bash shell is:

      $ echo “scale=100; a(1)*4” | bc -l

      You could be using another shell, you can let us know, above all thanks for your vital concerns and productive feedback.

    • Hello There,

      Thank you, Your blog is indeed quite interesting around Practical Interview Questions and Answers on Linux Shell Scripting! I agree with you on lot of points!

      By the way do you have any YouTube videos, would love to watch it. I would like to connect you on LinkedIn, great to have experts like you in my connection (In case, if you don’t have any issues).

      Thank you,

  3. Script for question#2 (adding two parameter numbers) uses expr $1 + $2` which is an OLD shell scripting syntax for addition, that has been around since 1973, and is less efficient because it uses a separate binary process just for the math.

    The preferred form in Korn/ksh shell or bash is: $(( $1 + $2 )) which runs faster because it uses the built-in math support from the shell, rather than separate process.

    Shell scripts using `expr` Will run OK, but is not considered ‘good form’ and most shell script writers will not give that answer… so I would not want somebody giving an interview to think that $(( $1 + $2 )) is in-correct.

    • @Joseph

      You are absolutely correct, Although they both methods work just fine, however, using $(( $1 + $2 )) is more efficient than `expr $1 + $2`. I often use the former for mathematical expressions in shell scripts.


Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.