linux bash interview questions

Bash Scripting is a powerful a part of system administration and development used at an extreme level. It’s utilized by the System directors, Network Engineers, Developers, Scientists, and everybody UN agency use Linux/Unix package. They use Bash for system administration, knowledge crunching, net application preparation, automatic backups, making custom scripts for numerous pages, etc.

In computer programing, a script could be a set of commands for an applicable run time atmosphere that is employed to alter the execution of tasks.

A Bash Shell Script could be a plain document containing a group of assorted commands that we have a tendency to typically sort within the command. Its accustomed alter repetitive tasks on UNIX system filesystem. It would embody a group of commands, or one command, or it would contain the hallmarks of imperative programming like loops, functions, conditional constructs, etc. Effectively, a Bash script could be a bug written within the Bash artificial language.

We had discussed the bash, script and bash scripting. Now, let’s move on to the FAQ’s and interview questions of Bash scripting.

The bash script is a shell programming language. Generally, we run many types of shell commands from the terminal by typing each command separately that require time and efforts. If we need to run the same commands again then we have to execute all the commands from the terminal again. But using a bash script, we can store many shell command statements in a single bash file and execute the file any time by a single command. Many system administration related tasks, program installation, disk backup, evaluating logs, etc. can be done by using proper bash script.

The variables which are pre-defined and maintained by the Linux operating system are called system variables. These type of variables are always used by an uppercase letter. The default values of these variables can be changed based on requirements.

The variable can be declared in bash by data type or without data type. If any bash variable is declared without declare command, then the variable will be treated as a string. Bash variable is declared with declare command to define the data type of the variable at the time declaration.

Single line and multi-line comments can be used in bash script. ‘#‘ symbol is used for single-line comment. ‘<<’ symbol with a delimiter and ‘:’ with single (‘) are used for adding multi-line comment.

String values can be combined in bash in different ways. Normally, the string values are combined by placing together but there are other ways in bash to combine string data.

`echo` and `printf` commands can be used to print output in bash. `echo` command is used to print the simple output and `printf` command is used to print the formatted output.

Command-line arguments are read by $1, $2, $3…$n variables. Command-line argument values are provided in the terminal when executing the bash script. $1 is used to read the first argument, $2 is used to read the second argument and so on.

“S” bit on a file causes the process to have the privileges of the owner of the file during the instance of the program.

For example, executing “passed” command to change current password causes the user to writes its new password to shadow file even though it has “root” as its owner.

12. Create a directory such that anyone in the group can create a file and access any person’s file in it but none should be able to delete a file other than the one created by himself.

We can create the directory giving read and execute access to everyone in the group and setting its sticky bit “t” on as follows:

14. How can any user find out all information about a specific user like his default shell, real-life name, default directory, when and how long he has been using the system?

$$ gives the process id of the currently executing process whereas $! Shows the process id of the process that recently went into the background.

These are the processes which have died but whose exit status is still not picked by the parent process. These processes even if not functional still have its process id entry in the process table.

We can use utilities like “ftp,” “scp” or “rsync” to copy a file from one machine to other.

18. I want to monitor a continuously updating log file, what command can be used to most efficiently achieve this?

We can use tail –f filename. This will cause only the default last 10 lines to be displayed on std o/p which continuously shows the updating part of the file.

19. I want to connect to a remote server and execute some commands, how can I achieve this?

20. I have 2 files and I want to print the records which are common to both.

We can use tail –f filename. This will cause only the default last 10 lines to be displayed on std o/p which continuously shows the updating part of the file.

21. I want to connect to a remote server and execute some commands, how can I achieve this?

22. I have 2 files and I want to print the records which are common to both.

26. I want to read all input to the command from file1 direct all output to file2 and error to file 3, how can I achieve this?

exec” overlays the newly forked process on the current process; so when I execute the command using exec, the command gets executed on the current shell without creating any new processes.

E.g., Executing “exec ls” on command prompt will execute ls and once ls exits, the process will shut down

egrep is Extended grep that supports added grep features like “+” (1 or more occurrence of a previous character),”?”(0 or 1 occurrence of a previous character) and “|” (alternate matching)

du -s /home/user1 ….where user1 is the user for whom the total disk space needs to be found.

38. Write a command sequence to find all the files modified in less than 2 days and print the record count of each.

In bash a function or procedure is called a subroutine. The declaration and calling of a subroutine in bash is different from other languages. No argument can be declared in subroutines unlike other standard programming languages. But local variables can be defined within the subroutine by using the ‘local’ keyword.

Bash has no built-in function like other languages to cut some portion of the string data. But using parameter expansion any string value can be cut in bash. Three parts can be defined in parameter expansion by separating with a colon to cut any part of the string data. Here, the first two parts are mandatory and the last part is optional. The first part contains the main string variable that will be used to cut, the second part is the starting position from where the string will be cut and the third part is the length of the cutting string. The starting position must be counted from 0 and the length must be counted from 1 of the main string to retrieve the cutting value.

Arithmetic operations can be done in multiple ways in bash. ‘let’, ‘expr’, ‘bc’ and double brackets are the most common ways to perform arithmetic operations in bash. The uses of these commands are shown in the following example.

Bash has many test commands to check if a file or directory exists or not and the type of the file. ‘-d’ option is used with a directory path as a conditional statement to check if the directory exists or not in bash. If the directory exists, then it will return true otherwise it will return false.

Using ‘exit’ command, a bash script can be terminated without executing all statements. The following script will check if a particular file exists or not. If the file exists, then it will print the total characters of the file and if the file does not exist then it will terminate the script by showing a message.

break statement is used to terminate from a loop without completing the full iteration based on a condition and continue statement is used in a loop to omit some statements based on a condition. The uses of break and continue statements are explained in the following example.

Executable bash files can be made by using ‘chmod’ command. Executable permission can be set by using ‘+x’ in chmod command with the script filename. Bash files can be executed without the explicit ‘bash’ command after setting the execution bit for that file.

46. What is meant by ‘bc’ and how can this command can be used in bash?

The full form of ‘bc’ is Bash Calculator to perform arithmetic operations more accurately in bash. The fractional part is omitted if any arithmetic operation is done in bash by using ‘expr’ command. The fractional part can be rounded also by using scale value with ‘bc’ command.

There are many ways to print a particular line in bash. How the ‘awk’, ‘sed’ and ‘tail’ commands can be used to print a particular line of a file in bash is shown in the following example.

it acts as delimiter to separate the word from the line of text. It is mainly used for splitting a string, reading a command, replacing text etc.

‘expr’, ‘wc’ and ‘awk’ commands can be used to find out the length of a string data in bash. ‘expr’ and ‘awk’ commands use length option, ‘wc’ command uses ‘–c’ option to count the length of the string.

Multiple bash scripts can be executed in parallel by using nohup command. How multiple bash files can be executed in parallel from a folder is shown in the following example.

51. How can conditional statements be used in bash? The most common conditional statement in most programming languages is the if-elseif-else statement. The syntax of if-elseif-else statement in bash is a little bit different from other programming languages. ‘If’ statement can be declared in two ways in a bash script and every type of ‘if’ block must be closed with ‘fi’. ‘if’ statement can be defined by third brackets or first brackets like other programming languages

This article has all the interview questions and answers of Bash scripting. All the possible FAQ’s are mentioned for the readers. If you are one of them who wants to make their career in bash scripting and want to become a bash script programmer in future, then you can vist devopsschool website for better options.

Bash Scripting Interview Questions

#0 What is bash script?

The bash script is a shell programming language. Generally, we run many types of shell commands from the terminal by typing each command separately that require time and efforts. If we need to run the same commands again then we have to execute all the commands from the terminal again. But using a bash script, we can store many shell command statements in a single bash file and execute the file any time by a single command. Many system administration related tasks, program installation, disk backup, evaluating logs, etc. can be done by using proper bash script.

#0 What are the advantages of using bash scripts?

Bash script has many advantages which are described below:

  • It is easy to use and learn.
  • Many manual tasks that need to run frequently can be done automatically by writing a bash script.
  • The sequence of multiple shell commands can be executed by a single command.
  • Bash script written in one Linux operating system can easily execute in other Linux operating system. So, it is portable.
  • Debugging in bash is easier than other programming languages.
  • Command-line syntax and commands that are used in the terminal are similar to the commands and syntax used in bash script.
  • Bash script can be used to link with other script files.
  • Q What is a Shell Script? Can you name some of its advantages?

    A shell script is a command-containing text-file that contains commands in order of their execution. Typical operations performed by shell scripts include printing text, file manipulation, and program execution.

    Following are the two main advantages of shell scripting:

  • It facilitates developing your own custom OS with relevant features which best suit your needs.
  • It facilitates designing software applications according to their respective platforms.
  • Career growth

    There are a lot of opportunities from many reputed companies in the world. According to research, Linux Shell Scripting has a market share of about 17. Since 2018, Linux has started to expand its market rapidly. According to PayScale, the average salary for shell scripting skill is $81,951. If you want career growth in DevOps, System administrator, learning shell scripting interview questions would be a great start.

    FAQ

    What is Bash used for in Linux?

    Software developers rely on Bash for many development tasks. Bash can be used to automate software development tasks such as code compilation, debugging source code, change management and software testing. Network engineers use Bash to test, configure and optimize network performance on organizational networks.

    What is the difference between $$ and $!?

    The bash shell
    • Short for “Bourne-Again Shell,” bash is a Unix shell. …
    • Bash is the default shell in macOS, Windows Subsystem for Linux, and the majority of Linux operating systems. …
    • bash is a sh-compatible command language interpreter that executes commands read from the standard input or from a file.

    What is the difference between Bash and shell?

    18: What is the difference between $$ and $!?

    $$ gives the process id of the currently executing process whereas $! Shows the process id of the process that recently went into the background.

    Related Posts

    Leave a Reply

    Your email address will not be published. Required fields are marked *