Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How to use pipe commands?

Answer»

Pipe command allows you to use several commands in the same way, in which the output of one is used as input for another. Like a pipeline, each process output is directly input to the next one. A pipe is represented by the symbol "|". The flow of data through a pipeline is UNIDIRECTIONAL, i.e., from left to right.

Syntax :

command_1 | command_2 | command_3 | .... | command_NConclusion 

You can automate a lot of repetitive manual tasks with shell scripting, which is an easy and powerful programming method. Modern programming languages offer many of the features that make them appealing to programmers. DevOps engineers and automation testers often find this concept vital in preparing for interviews. Shell scripting allows the development of simple to complex scripts. A number of daily tasks can be AUTOMATED using shell scripting as well. We THOUGHT of making your job easier by putting TOGETHER an ensemble of the most frequently asked interview questions and sample answers on shell scripting. These interview questions will DEFINITELY help you understand that shell is basically an interface between the user and the operating system, which interprets user commands to be executed by the kernel or operating system.

Useful Resources:

Unix Interview

Automation Testing Interview

Devops Interview

2.

How can we create a function in shell script?

Answer»

In other programming languages, shell FUNCTIONS are much like subroutines, procedures, and functions. The syntax for declaring a function is as FOLLOWS:

function_name () { list of commands}

Function_name is the name of your function, and that's what you'll use to call it from anywhere in your script. The function name must be followed by PARENTHESES, then a list of commands enclosed in braces.

Example:

The FOLLOWING example shows how function can be used:

#!/bin/sh# Define your function here Hello () { echo "Hello World" } # Invoke your functionHello

The following output will be displayed after execution:

$./test.shHello World
3.

Write difference between grep and find command.

Answer»
  • Grep command: This command facilitates searching and displaying content based on regular expressions specified by the user. Using the Grep command, one can search in a file for PATTERNS. Grep command have the following syntax:
    grep “literal string” <filename>

Example: 

grep “apple” file1.txt //Displays all the lines with the word “apple” in the file1grep “apple” file1.txt  file2.txt //Scan MULTIPLE documents and search the word “apple” in both files.
  • Find command: The FIND command searches for files and folders based on their size, modification time, and access time. You can use this command to search files and directories. Find command have the following syntax:
    find <path> <search criteria> <action>

Example:

find –type  f  // Command will find all the filesfind  –type  d  //Command will find all the directoriesfind .  –name file1.txt //Command will find file1.txt in the CURRENT directory.
4.

Write the difference between $$ and $!?

Answer»

You can USE $$ to get the PROCESS ID of the current process. However, $! displays the process id for a background process that recently WENT away.

5.

Name the command that one should use to know how long the system has been running.

Answer»

Using the Uptime command, you can see how long your system has been ACTIVE. You can also determine the number of users with RUNNING sessions, and the AVERAGE system load for 1, 5, and 15 minutes. The information displayed at once can also be filtered based on your specified options. 

Example: $ uptime   

Entering the above command at the shell prompt, namely $ uptime, will PRODUCE the following output:

9:21am  up 86 day(s), 11:46,  3 users,  load average: 2.24, 2.18, 2.16

6.

Explain how you will open a read-only file in Unix/Shell.

Answer»

You can OPEN a read-only file by: vi –R &LT;Filename&GT;

7.

How will you find total shells available in your system?

Answer»

Within your system, there are several shells AVAILABLE. If you want to find all the shells on the system, you can USE $ CAT /etc/shells. 

Example: 

$ cat /etc/shells 

EXECUTION over SHELL Interpreter/Editor: 

$ cat /etc/shells 

Output: 

/bin/sh 

/bin/bash 

/sbin/nologin 

/bin/ksh 

/bin/csh

8.

What do you mean by metacharacters?

Answer»

In a DATA FIELD or program, metacharacters are special characters that provide information about other characters. In SHELLS, they're called regular expressions.  A character that is neither a letter nor a number is generally considered a metacharacter. Using shell metacharacters, you can group together commands, redirect and pipe input/output, put commands in the background, reduce the size of file names and path names, etc. You can USE them as wildcards to specify a file name without typing in the file's full name. The most COMMON metacharacters are as follows:

  • Asterisk (*): Use the * to match any character.
  • Question Mark (?): It matches a single character in the filename.
  • Brackets ([…]):  The metacharacters used here match some specified characters.
  • Hyphen (-): When placed within [] (brackets), the - metacharacter matches a specified range.
9.

Name different commands that are available to check the disk usage.

Answer»

The following commands are available to CHECK disk space USAGE:

  • df – It is USED to FIND out how much space is left on a disk.
  • du – With this command, you can find out how much disk space the specified files and each subdirectory take up.
  • dfspace – Using this command, you can check the amount of free disk space in MB.
10.

Name the command that is used to display the shell's environment variable.

Answer»

Shell functions, along with other Linux programs, are controlled by environmental variables.   Any child PROCESS of the shell has access to an ENVIRONMENT variable. These variables are NECESSARY for some programs to function properly. ENV or printenv commands can be used to DISPLAY the shell's environment variables.

11.

Write the difference between “=” and “==”.

Answer»
  • = operator: Assigning the value into a variable is accomplished by using the = operator. It is REFERRED to as the assignment operator.
    Example: Suppose variable a holds 5 and variable B holds 2, then: 
a = $b;  #Would assign value of b to a
  • == operator: This is used to compare strings. In the double equals operator, both operands are COMPARED. If they are equal, it returns true, otherwise, it returns false.
    Example: Suppose variable a holds 5 and variable b holds 2, then:
[ $a == $b ];  #Comparing the values of a and b.
12.

What is the importance of sed command and awk command?

Answer»
  • sed Command: sed command is an acronym for stream editor. It is used for editing a file without using an editor. The SED command performs a variety of operations on files, such as searching, finding and replacing, inserting and deleting. However, substitution or find and replace are the most commonly used features of SED.
    Syntax: sed options file 
    Example: Execution over Shell Interpreter/Editor
/u/user1/Shell_Scripts_2020> echo "Hi Gourav" | sed 's/Hi/Hello/'

In this case, “s” command in sed replaces the string Hi with “Hello”. 

Output: 

Hello Gourav
  • AWK Command: The command is a scripting language generally used to manipulate data and generate reports. There is no need to compile it and users are ALLOWED to have access to a variable, string functions, numeric functions, and logical operations.
    Syntax: awk options File Name 
    Example: Script/Code
/u/user1/Shell_Scripts_2017> cat > script10 Hello Aisha Hello Rohan Hello Gourav

An awk command/utility assigns variables in following way: 

$0 -> For whole LINE (e.g. Hello Aisha)  

$1 -> For the FIRST FIELD i.e. Hello  

$2 -> For the second field 

Execution over Shell Interpreter/Editor 

awk ‘{print $0}’ script10

The above script prints all the 3 lines completely. 

Output: 

Hello Aisha Hello Rohan Hello Gourav

Execution over Shell Interpreter/Editor 

awk ‘{print $1}’ script10

The above script prints only the first word i.e., Hello from each line. 

Output: 

Hello Hello Hello
13.

What is the best way to debug the shell script/program problems?

Answer»

Following are some common methods of DEBUGGING a script:

  • The SHELL script can be MODIFIED to include DEBUG statements to display the information that can be useful in identifying the problem.   
  • By setting -x in the script, we can ENABLE debugging.
14.

How will you debug a shell script?

Answer»

With set, you can turn on or off DEBUGGING OPTIONS in the SHELL

  • Set -x: This displays commands and their arguments as they are being executed.
  • Set -V: It displays shell INPUT lines in real-time as they are read.
15.

What do you mean by the “s” permission bit in a file?

Answer»

The SUID (Set User ID) bit is known as the "s" bit. SUIDS are special FILE permissions for executable files that enable other USERS to run the file as the file owner. There will be an s (to INDICATE SUID) special permission for the user instead of the normal x, which represents EXECUTE permissions. A file that has the "s" bit set will grant the process the rights of the file's owner while the program is running.

Example: 

Executing the "passwd" COMMAND to change the password, for example, allows the user to write the new password to the shadow file even though its owner is "root".

16.

How will you pass and access arguments to a script in Linux?

Answer»

In SCRIPTS, ARGUMENTS are passed as FOLLOWS

scriptName "Arg1" "Arg2"…."ARGN

Arguments in a script can be ACCESSED as follows: 

 $1 , $2 .. $n 

17.

What are interactive and non-interactive shells?

Answer»

Interactive SHELL: /bin/bash and /bin/sh is interactive shell. It is a non-login shell that gets started from a COMMAND line. It first copies the parent ENVIRONMENT and then invokes.

Non-Interactive shell: /sbin/nologin shell is non-interactive shell. It is PRESENT when the shell SCRIPT is running and just inherits the parent's environment.

18.

Write down the Syntax for all the loops in Shell Scripting.

Answer»

In shell scripting, you can USE three looping statements as given below:

  • while statement
  • for statement
  • until statement

Syntax is as follows:  

  • For Loop: Loops that operate on LISTS of items are known as loops. Each item in the list receives the same set of commands.

Syntax: 

 for VAR in word1 word2 ... wordN do Statement to be executed done

Example: 

for a in 1 2 3 4 5 6 7 8 9 10 do if [ $a == 3 ] then break fi echo "Iteration no $a" done

Output” 

$bash -f main.sh Iteration no 1 Iteration no 2
  • While Loop: It involves evaluating a command first and then executing a loop based on the result. The loop will be terminated if the command raises to FALSE.

Syntax:  

while command do Statement to be executed done

Example:  

a=0 while [ $a -lt 3 ] do echo $a a=`expr $a + 1` done

Output: 

$bash -f main.sh 0 1 2 3
  • Until Loop: As OFTEN as the condition/command evaluates to false, the until loop is executed. Once the condition or command becomes true, the loop terminates.

Syntax: 

until command do Statement to be executed until command is true done

Example: 

a=0 until [ $a -gt 3 ] do echo $a a=`expr $a + 1` done

Output: 

$bash -f main.sh 0 1 2 3
19.

Name the command that is used to compare the strings in a shell script.

Answer»

To compare text strings, use the test command. By COMPARING each character in each STRING, the test command usually COMPARES text strings.  In most cases, tests are generally included as a conditional execution of shell commands. It can be used for:

  • Comparison of file attributes
  • Comparison of strings
  • Comparing basic arithmetic operations

Using square brackets ([ and ]) around the EXPRESSION is equivalent to testing it with the test. 

Syntax of test command: 

  • test EXPRESSION
  • test EXPRESSION && true-command
  • test EXPRESSION || false-command
  • test EXPRESSION && true-command || false-command

Examples:  

  • test 50 -gt 40 && echo "Yes" || echo "No"

Because 50 is greater than 40, this command prints the text "Yes".

  • ["Excellent" = "Excellent"]; echo $?

As the two strings are identical, this command prints "0" because the expression is true.

20.

Write the difference between $* and $@

Answer»

Unlike $@, where each QUOTED argument is TREATED as a SEPARATE argument, $* treats each positional parameter as a single argument.

21.

Name the command that is used to take the backup.

Answer»

The backup is TAKEN using the TAR command. Tar is an ACRONYM for tape archive and is used to create backups using tar, gzip, and bzip. FILES can be saved and restored from and to a tape using this command. In this, files and directories are generally compressed into tarballs, an archive file. For this purpose, it is among the most widely used commands. Furthermore, the tarball can be easily moved from one server to another.

22.

Name two files of crontab command.

Answer»

cron.deny and cron.allow are two FILES in the /etc/cron.d directory that controls access to the crontab COMMAND. Crontab command tasks such as creating, editing, displaying, or removing crontab files are relegated to specific users through these files. Both files usually consist of a list of user NAMES, one user name per line. Together, these access control files perform the following functions:

  • cron.allow decides which users are allowed to run the crontab command.
  • cron.deny decides which users are DENIED from using the crontab command.
  • When cron.allow or cron.deny doesn't EXIST, superuser privileges are required to run it.
23.

What do you mean by crontab?

Answer»

Crontab stands for "cron table," meaning that it makes use of the cron scheduler to perform tasks. In other words, it is the list of commands that you WISH to run on a regular schedule and the command that will let you MANAGE it. It is possible to VIEW or EDIT the table of commands using the crontab command. In addition to the schedule, the term "Crontab" also refers to the name of the program USED to edit the schedule.