
Brief Introduction of Bash Scripting
The full form of Bash is Bourne Again Shell. It is a shell program. We will discuss Bash Scripting Interview Questions here. It is also an open-source GNU project. It is basically used for programming and interactive uses. It works as a command-line interpreter, where the combination of commands is known as Shell Script. It can also read and execute the command from a shell script.
Bash Scripting Interview Questions
- Give some features of Bash.
Features of Bash Scripting
- Bash is shell-compatible as it is having the features of Korn and C shells also.
- Bash scripting can be called or invoked by a single character command and also by multiple characters command.
- Each particular file in bash is unique and performs specific functions.
- Bash consists of Key bindings.
- It operates with a one-dimensional array to modify data easily.
- It also consists of some restricted modes while working.
- What is a Shell? What are the different types of shells available in the Linux Operating System?
A Shell is a type of program or command interpreter, which interprets the commands given by a user directly or by reading a file. Then it passes the output to an operating system for further processing.
There are various different types of Shells:
- Bourne Shell
- C Shell
- Korn Shell
- GNU Bourne Shell
- What do you understand by SheBang [#!]?
SheBang is also known as Path Directive for executive Bash, python, or other types of scripts. It consists of a number sign and an exclamation mark at the end. If we use this character sequence under a Unix-like operating system, the program loader parses the remaining line with the interpreter directive.
- What are absolute and relative paths?
The absolute path of any file is the root location of that file. It starts with a Slash[\] and contains the parent directory on the left side, followed by the child directory on the right side. We can get the absolute path by using the PWD command. It is complex as compared to a relative path. Relative path of any file is the current location of that file. It does not start with a Slash[\]. It just gives the current path where the file is opened. This mode is easier as compared to the absolute path.
- Explain the working of Bash variables.
There are two main actions performed by the variables in bash. They are as follows:
- Setting the value of a variable
- Reading the value of a variable
The two types of working are:
- Working on Terminal
- Working on Bash Shell
- What do you understand by Command Substitution?
Command Substitution is a term where the shell takes the command, an output is produced, and the command itself replaces that output. It is convenient when we have a single command line output because, in this case, no trailing newlines are detected.
- What is the use of the sleep command in Bash Script?
The function of the sleep command is to insert delays or pause the program’s execution for a specific period of time. Basically, it is used when the programmer wants to pause the execution of any command for a specific interval of time. The delay can be made for seconds, minutes, hours, or days.
- What is the difference between Zsh shell and Bash shell?
Zsh Shell:
- It is not installed by default in Unix/Linux machines.
- Tab completion features are much faster and easier.
- It has a built-in spelling correction feature.
- It provides support for various plugins.
- The “which command” in the Zsh shell reveals the location of the command, the source of the function, and the definition of the alias.
Bash Shell:
- It is a built-in shell in Unix/Linux machines.
- It lacks some completion features as compared to the Zsh shell.
- It does not provide any spelling correction feature.
- It does not have various plugins for themes and features.
- The “which command” in the Bash shell reveals only the location of the command.
- What makes the array in Bash script different from other scripts?
Usually, the array is defined as a collection of similar elements, but in Bash scripting, arrays do not require a collection of similar elements. It can have different elements because bash does not differentiate strings from numbers. They both are treated similarly, which is why arrays in bash scripts are different from other scripts.
- What do you understand by a Bash Function?
Functions are usually called a group of statements that perform a specific task. A bash function is defined as a set of statements or commands used to reuse a particular code multiple times to reduce code length and increase the optimization speed. It prevents the programmer from writing the same code again and again.
- Explain overriding commands in Bash scripts and how it is useful?
Bash script provides a handy feature of overriding commands. It can be done by creating a function with the same name as the command we want to override. It is useful in those scenarios where we want to provide the whole command with options many times within that script.
- Define the types of file permissions in Bash scripting?
There are mainly three types of file permissions in Bash scripting:
- Read[r] – This is mainly used to view the content of the file.
- Write[w] – This is mainly used to modify the content of the file.
- Execute[x] – This is mainly used to compile and run the programs and the contents of a file.
- Explain hash command in Bash script.
Hash command is a built-in command in Bash. It is used to list hash tables consisting of recently viewed tables and files. It is responsible for storing the recently viewed files’ locations and showing us whenever we want to see them.
- How many types of comments do Bash script supports and how can you insert them?
Comments are an essential part of a program code. They are useful for defining the code when a person is going through the code. A programmer can use it in various ways to make his or her code efficient while reading. There are two types of comments:
- Single Line Comment
- Multiple or Multi-line Comment
Insertion of Single Line Comment
To insert a single line comment, the use of Hash[#] symbol takes place. Hash[#] symbols need to be added in the starting of the comment line.
Insertion of Multi-Line Comment
There are two methods to insert multi line comments:
- Method 1– Enclose comments between <<COMMENTS and COMMENTS.
Example:<<COMMENTS Statement/The Comment COMMENTS
- Method 2 – Enclose comments between :’ and ‘.
Example: ;’ Statement/the comment ‘
- What are the quotes in Bash? Define their types.
Quotes in Bash are used when the programmer wants to store more complex values in a variable. They handle texts and names of the files using space characters because bash accepts space characters to differentiate among separate items. The contents in quotes are always considered as a single item.
There are two types of quotes in Bash:
- Quote with string – While using texts or strings, both single and double quotes are suitable. For example, ‘hello user’ or “hello user.”
- Quote with variable – While defining a string of text to a variable, only double quotes are acceptable—for example, name=” Hello user,” where name is the variable.
- What is the use of [#] symbol?
The # symbol is used to find the length of the string. For example, if we want to find the length of a string “Welcome to Bash Scripting Interview Question.”
Code:
name= “Welcome to Bash Scripting Interview Question”
length=’expr length ”$name”’
echo “Length of ‘$name’ is ‘$length”
Output: Length of “Welcome to Bash Scripting Interview Question” is 39
- What do you understand by $IFS?
Internal Field Separator, $IFS is a very special internal variable, and it is used to split the provided strings into words. The white spaces in the given string are the separating points by default. But one can always set the delimiter $IFS=’. ‘ The separating points can be space, comma, symbol, etc.
- What are the advantages and disadvantages of Bash Scripting?
Advantages
- The syntax and structure of Bash scripts are easy to understand.
- There are various commands which are general and common; hence they are easy to remember.
- As it is an interpreted language, so compilation does not require before running.
- Highly efficient.
Disadvantages
- Input and Output performance is not that good. It is low as compared to other languages.
- The inspection of variables is not strict.
- It usually does not come pre-installed in windows, so there are chances that the code may not be compatible with the operating system.
- Bash does not support many functions, for example- objects, data structures, etc.
- Poor debugging tools.
Also read Ajax Interview Questions (10+ Sample Answers)