10 Useful ‘Interview Questions and Answers’ on Linux Shell Scripting

Greeting of the day. The vastness of Linux makes it possible to come up with a unique post every time. We ‘The-Tecmint-Team‘ works to provide our readers with unique contents which is useful for them from career perspective as well as adding to the Knowledge base. Here is an attempt and it is on our readers to judge how far we succeed.

Questions on Shell Scripting
Questions on Shell Scripting

We have lots of tutorials on Shell Scripting language and Interview Questions for readers of all kind, here are the links to those articles.

  1. Shell Scripting Series
  2. Interview Question and Answer Series

Adding to the shell scripting posts here, in this article we will be going through questions related to Linux Shell from interview point of view.

1. How will you abort a shell script before it is successfully executed?
Answer : We need to use ‘exit’ command to fulfil the above described situation. A ‘exit’ command when forced to output any value other than 0 (zero), the script will throw an error and will abort. The value 0 (zero) under Unix environment shell scripting represents successful execution. Hence putting ‘exit -1’, without quotes before script termination will abort the script.

For example, create a following shell script as ‘anything.sh‘.

#!/bin/bash
echo "Hello"
exit -1
echo "bye"

Save the file and execute it.

# sh anything.sh

Hello
exit.sh: 3: exit: Illegal number: -1

From the above script, it is clear that the execution went well before exit -1 command.

2. How to remove the headers from a file using command in Linux?
Answer : A ‘sed’ command comes to rescue here, when we need to delete certain lines of a file.

Here it the exact command to remove headers from a file (or first line of a file).

# sed '1 d' file.txt

The only problem with above command is that, it outputs the file on standard output without the first line. In order to save the output to file, we need to use redirect operator which will redirects the output to a file.

# sed '1 d' file.txt > new_file.txt

Well the built in switch ‘-i‘ for sed command, can perform this operation without a redirect operator.

# sed -i '1 d' file.txt
3. How will you check the length of a line from a text file?
Answer : Again ‘sed’ command is used to find or check the length of a line from a text file.

A ‘sed –n ‘n p’ file.txt‘, where ‘n‘ represents the line number and ‘p‘ print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option. So, how to get the length count? Obviously! we need to pipeline the output with ‘wc‘ command.

# sed –n 'n p' file.txt | wc –c

To get the length of line number ‘5’ in the text file ‘tecmint.txt‘, we need to run.

# sed -n '5 p' tecmint.txt | wc -c
4. Is it possible to view all the non-printable characters from a text file on Linux System? How will you achieve this?
Answer : Yes! it is very much possible to view all the non-printable characters in Linux. In order to achieve the above said scenario, we need to take the help of editor ‘vi’.

How to show non-printable characters in ‘vi‘ editor?

  1. Open vi editor.
  2. Go to command mode of vi editor by pressing [esc] followed by ‘:’.
  3. The final step is to type execute [set list] command, from command interface of ‘vi’ editor.

Note: This way we can see all the non-printable characters from a text file including ctrl+m (^M).

5. You are a Team-Leader of a group of staffs working for a company xyz. The company ask you to create a directory ‘dir_xyz’, such that any member of the group can create a file or access a file under it, but no one can delete the file, except the one created it. what will you do?
Answer : An interesting scenario to work upon. Well in the above said scenario we need to implement the below steps which is as easy as cake walk.
# mkdir dir_xyz
# chmod g+wx dir_xyz
# chmod +t dir_xyz

The first line of command create a directory (dir_xyz). The second line of command above allow group (g) to have permission to ‘write‘ and ‘execute‘ and the last line of the above command – The ‘+t‘ in the end of the permissions is called the ‘sticky bit‘. It replaces the ‘x‘ and indicates that in this directory, files can only be deleted by their owners, the owner of the directory or the root superuser.

6. Can you tell me the various stages of a Linux process, it passes through?
Answer : A Linux process normally goes through four major stages in its processing life.

Here are the 4 stages of Linux process.

  1. Waiting: Linux Process waiting for a resource.
  2. Running : A Linux process is currently being executed.
  3. Stopped : A Linux Process is stopped after successful execution or after receiving kill signal.
  4. Zombie : A Process is said to be ‘Zombie’ if it has stopped but still active in process table.
7. What is the use of cut command in Linux?
Answer : A ‘cut’ is a very useful Linux command which proves to be helpful when we need to cut certain specific part of a file and print it on standard output, for better manipulation when the field of the file and file itself is too heavy.

For example, extract first 10 columns of a text file ‘txt_tecmint‘.

# cut -c1-10 txt_tecmint

To extract 2nd, 5th and 7th column of the same text file.

# cut -d;-f2 -f5 -f7 txt_tecmint
8. What is the difference between commands ‘cmp’ and ‘diff’?
Answer : The command ‘cmp’ and ‘diff’ means to obtain the same thing but with different mindset.

The ‘diff‘ command reports the changes one should make so that both the files look the same. Whereas ‘cmp‘ command compares the two files byte-by-byte and reports the first mismatch.

9. Is it possible to substitute ‘ls’ command with ‘echo’ command?
Answer : Yes! the ‘ls’ command can be substituted by ‘echo’ command. The command ‘ls’ lists the content of file. From the point of view of replacement of above command we can use ‘echo *’, obviously without quotes. The output of both the commands are same.
10. You might have heard about inodes. can you describe inode briefly?
Answer : A ‘inode’ is a ‘data-structure’, which is used for file identification on Linux. Each file on an Unix System has a separate ‘inode’ and an ‘Unique’ inode Number.

That’s all for now. We will be coming up with another interesting and knowledgeable Interview questions, in the next article. Till then Stay tuned and connected to Tecmint.com. Don’t forget to provide us, with your 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!

Avishek
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.

24 Comments

Leave a Reply
  1. I’ve found a little mistake in the 8th question about cut command.

    In the first example, the -c option is used and it extracts characters not columns as stated.

    Reply
    • The way the question was asked wasn’t very clearly stated to accomplish the use of the command ls and echo to answer it in the affirmative and explain.

      The question should have been, “Is it possible to list the file and directory names of a given ‘parent directory’ using a command other than ‘ls?'”

      Yes, the use of “echo *” will perform the equivalent of a plain “ls.”

      Reply
  2. Hi,

    I need to execute a script on remote as a sudo user or root user from my local VM. It is not password less login. we need to pass password thorugh command line.

    #!/bin/bash
    sshpass -p “xxxx” ssh -o StrictHostKeyChecking=no [email protected] <<EOF
    echo "password" | 'sudo su -'
    sh script.sh
    EOF

    Note : there is no sshpass in remote server and we cannot install it for security reason.

    I am able to login to remote user as user but i can't login as a sudo user , Please help me how to achieve this. i have seen many quires but none one helped me

    -Thanks
    Jagan

    Reply
    • @Jagan,

      In CentOS, there isn’t any sudo user by default and the all the programs run with root privileges as root user, if you want to run enable sudo for user, you shoud use visudo command to enable it as shown:

      # visudo
      

      And add this entry to enable sudo privileges to your user.

      yourusername ALL=(ALL) ALL
      
      Reply
  3. I need to execute a script on remote as a sudo user or root user from my local VM. It is not password less login. we need to pass password thorugh command line.

    #!/bin/bash
    sshpass -p “xxxx” ssh -o StrictHostKeyChecking=no [email protected] <<EOF
    echo "password" | 'sudo su -'
    sh script.sh
    EOF
    Note : there is no sshpass in remote server and we cannot install it for security reason.

    I am able to login to remote user as user but i can't login as a sudo user , Please help me how to achieve this. i have seen many quires but none one helped me

    -Thanks Jagan

    Reply
    • @Jagan,

      Have you enabled sudo privileges to user you trying to login into the server? if not, first add users to sudoers file with the help of visudo command.

      Reply
  4. Hello tecmint,

    I need a solution .for the below

    Write a Shell program to print the numbers in the following format:
    Use While or Until loop for the following program

    1  2  3  4
    2  3  4  5
    3  4  5  6
    4  5  6  7
    5  6  7  8
    6  7  8  9
    

    My Written Script in Until Loop, kindly correct where i have made a mistake

    #!/bin/bash
    w=1
    until [ $w -le 9 ] ;  do
         until [ $w -eq  4 ] ; do
      w=`expr $w + 1`
      echo "$w"
       done
    echo "$w"
    done
    

    Regards,
    Sunny

    Reply
    • i=1
      while [ $i -lt 7 ]
      do
      m=`echo $i`
      for n in 1 2 3 4
      do
      printf “$i”
      i=`expr $i + 1`
      done
      i=`expr $m + 1`
      printf “\n”
      done

      Reply
      • I’m a bit late to the party, but something more concise might be:

        i=1
        until [ $i -gt 6 ];
        do
          printf "%d %d %d %d\n" $i $((i+1)) $((i+2)) $((i+3))
          : $((i++))
        done
        
        Reply
  5. Point 6 is little mis-infomative.

    — “Zombie : A Process is said to be ‘Zombie’ if it has stopped but still active in process table.” —

    Technically a process cannot be active after it have been stopped, Actually Zombie is a process when parent process is killed/stopped but the child process still exists, That child process is known as Zombie process.

    Reply
  6. No, the ‘ls’ command cannot be substituted by ‘echo’ command.

    The echo command prints strings on the command line to standard output.

    When you run ‘echo *’, you are not running an ls command.

    What happens is that the shell expands the ‘*’ regular expression, replacing it with the list of files and directories in the current working directory.

    The echo command merely prints the result as a list of strings.

    The ls command lists the files or directories listed on the command line.

    You can more easily see the difference when you run the commands in a directory with no files or subdirectories in it that don’t being with a ‘.’.

    Reply
    • not as fruitful as ls in all aspect but for simply printing the contents of a directory you may use

      $ echo * and it will print the contents, but again not as versatile as ‘ls’

      Reply
  7. Thanks a lot, very usefull.
    for the sticky bit question, where should i create the directory ?
    I’ve created a directory in my home /home/red
    i’ve created as you said a directory inside as you told, but when i log as another user and try to edit the sticky directory, there is a permission problem!!
    thanks

    Reply
  8. clarification regarding
    How will you abort a shell script before it is successfully executed?
    for the above question exit 1[any non zero will work other than zero].please correct me if i am wrong?

    Reply
  9. Hi avishek

    How can we select all the text in one shot while working in vi editor
    i frequently need to copy & past text to multiple files

    Regards
    Rupesh K

    Reply

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.