{"id":26426,"date":"2022-02-08T12:57:57","date_gmt":"2022-02-08T12:57:57","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=26426"},"modified":"2022-12-22T14:38:57","modified_gmt":"2022-12-22T14:38:57","slug":"top-50-interview-question-and-answers-of-bash-scripting","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/top-50-interview-question-and-answers-of-bash-scripting\/","title":{"rendered":"Shell Bash Scripting Interview Questions and Answer \u2013 Part 4"},"content":{"rendered":"\n<p>Bash Scripting is a powerful a part of system administration and development used at an extreme level. It\u2019s 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.<\/p>\n\n\n\n<p><strong>Script<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/parts-of-a-script-1-728.jpg\" alt=\"\" class=\"wp-image-26448\" width=\"952\" height=\"714\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/parts-of-a-script-1-728.jpg 728w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/parts-of-a-script-1-728-300x225.jpg 300w\" sizes=\"auto, (max-width: 952px) 100vw, 952px\" \/><\/figure>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><strong>Bash Script:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"796\" height=\"311\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/QfP3Z.png\" alt=\"\" class=\"wp-image-26447\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/QfP3Z.png 796w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/QfP3Z-300x117.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/QfP3Z-768x300.png 768w\" sizes=\"auto, (max-width: 796px) 100vw, 796px\" \/><\/figure>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>We had discussed the bash, script and bash scripting. Now, let&#8217;s move on to the FAQ&#8217;s and interview questions of Bash scripting.<\/p>\n\n\n\n<p><strong>Interview questions and answers for Bash scripting<\/strong>:-<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>What is bash script?<\/strong><\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/maxresdefault-1024x576.jpg\" alt=\"\" class=\"wp-image-26446\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/maxresdefault-1024x576.jpg 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/maxresdefault-300x169.jpg 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/maxresdefault-768x432.jpg 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/maxresdefault-355x199.jpg 355w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/maxresdefault.jpg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>2. <strong>What are the advantages of using bash scripts?<\/strong><\/p>\n\n\n\n<p>Bash script has many advantages which are described below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is easy to use and learn.<\/li>\n\n\n\n<li>Many manual tasks that need to run frequently can be done automatically by writing a bash script.<\/li>\n\n\n\n<li>The sequence of multiple shell commands can be executed by a single command.<\/li>\n\n\n\n<li>Bash script written in one Linux operating system can easily execute in other Linux operating system. So, it is portable.<\/li>\n\n\n\n<li>Debugging in bash is easier than other programming languages.<\/li>\n\n\n\n<li>Command-line syntax and commands that are used in the terminal are similar to the commands and syntax used in bash script.<\/li>\n\n\n\n<li>Bash script can be used to link with other script files.<\/li>\n<\/ul>\n\n\n\n<p>3. <strong>Mention the disadvantages of bash scripts<\/strong><\/p>\n\n\n\n<p>Some disadvantages of bash script are mentioned below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It works slower than other languages.<\/li>\n\n\n\n<li>The improper script can damage the entire process and generate a complicated error.<\/li>\n\n\n\n<li>It is not suitable for developing a large and complex application.<\/li>\n\n\n\n<li>It contains less data structure compare to other standard programming languages.<\/li>\n<\/ul>\n\n\n\n<p>4. <strong>What types of variables are used in bash?<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"322\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/Linux-Shell-Variables-Meaninigs-1024x322-1.png\" alt=\"\" class=\"wp-image-26449\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/Linux-Shell-Variables-Meaninigs-1024x322-1.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/Linux-Shell-Variables-Meaninigs-1024x322-1-300x94.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/Linux-Shell-Variables-Meaninigs-1024x322-1-768x242.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Two types of variables can be used in bash script. These are:<\/p>\n\n\n\n<p>System variables<\/p>\n\n\n\n<p>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.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">\n`set`, `env` <span class=\"hljs-keyword\">and<\/span> `printenv` commands can be used to <span class=\"hljs-keyword\">print<\/span> the <span class=\"hljs-keyword\">list<\/span> of system variables.\n<span class=\"hljs-comment\">#!\/bin\/bash<\/span>\n<span class=\"hljs-comment\"># Printing System Variables<\/span>\n \n<span class=\"hljs-comment\">#Print Bash shell name<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $BASH\n \n<span class=\"hljs-comment\"># Print Bash shell Version<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $BASH_VERSION\n \n<span class=\"hljs-comment\"># Print Home directory name<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $HOME\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>5<\/strong>. <strong>How to declare and delete variables in bash?<\/strong><\/p>\n\n\n\n<p>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.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">\u2013r, -i, -a, -A, -l, -u, -t <span class=\"hljs-keyword\">and<\/span> \u2013x options can be used with <span class=\"hljs-keyword\">declare<\/span> command to <span class=\"hljs-keyword\">declare<\/span> a variable with different data types.\n\n<span class=\"hljs-comment\">#!\/bin\/bash<\/span>\n \n<span class=\"hljs-comment\">#Declare variable without any type<\/span>\nnum=<span class=\"hljs-number\">10<\/span>\n \n<span class=\"hljs-comment\">#Values will be combined but not added<\/span>\nresult=$num+<span class=\"hljs-number\">20<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $result\n \n<span class=\"hljs-comment\">#Declare variable with integer type<\/span>\n<span class=\"hljs-keyword\">declare<\/span> -i num=<span class=\"hljs-number\">10<\/span>\n \n<span class=\"hljs-comment\">#Values will be added<\/span>\n<span class=\"hljs-keyword\">declare<\/span> -i result=num+<span class=\"hljs-number\">20<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $result\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>6. <strong>How to add comments in a bash script?<\/strong><\/p>\n\n\n\n<p>Single line and multi-line comments can be used in bash script. \u2018#\u2018 symbol is used for single-line comment. \u2018&lt;&lt;\u2019 symbol with a delimiter and \u2018:\u2019 with single (\u2018) are used for adding multi-line comment.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash<\/span>\n<span class=\"hljs-comment\">#Print the text &#91;Single line comment]<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Bash Programming\"<\/span>\n&lt;&lt;addcomment\nCalculate the sum\nOf two numbers &#91;multiline comment]\naddcomment\nnum=<span class=\"hljs-number\">25<\/span>+<span class=\"hljs-number\">35<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $num\n: <span class=\"hljs-string\">'\nCombine two\nString data &#91;multiline comment]\n'<\/span>\nString=<span class=\"hljs-string\">\"Hello\"<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $string<span class=\"hljs-string\">\" World\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>7. <strong>How can you combine strings in a bash script?<\/strong><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>#!\/bin\/bash<\/p>\n\n\n\n<p>#Initialize the variables<\/p>\n\n\n\n<p>str1=&#8221;PHP&#8221;<\/p>\n\n\n\n<p>str2=&#8221;Bash&#8221;<\/p>\n\n\n\n<p>str3=&#8221;Perl&#8221;<\/p>\n\n\n\n<p># Print string together with space<\/p>\n\n\n\n<p>echo $str1 $str2 $str3<\/p>\n\n\n\n<p>#Combine all variables and store in another variable<\/p>\n\n\n\n<p>str=&#8221;$str1, $str2 and $str3&#8243;<\/p>\n\n\n\n<p>#Combine other string data with the existing value of the string<\/p>\n\n\n\n<p>str+=&#8221; are scripting languages&#8221;<\/p>\n\n\n\n<p>#Print the string<\/p>\n\n\n\n<p>echo $str<\/p>\n\n\n\n<p>8. <strong>Which commands are used to print output in bash?<\/strong><\/p>\n\n\n\n<p>`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.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash<\/span>\n \n<span class=\"hljs-comment\">#Print the text<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Welcome to LinuxHint\"<\/span>\nsite=<span class=\"hljs-string\">\"linuxhint.com\"<\/span>\n<span class=\"hljs-comment\">#Print the formatted text<\/span>\nprintf <span class=\"hljs-string\">\"%s is a popular blog site\\n\"<\/span> $site\n\nHow to take input from the terminal in bash?\n`read` command is used in a bash script to take input from the terminal.\n\n<span class=\"hljs-comment\">#!\/bin\/bash<\/span>\n<span class=\"hljs-comment\">#Print message<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Enter your name\"<\/span>\n<span class=\"hljs-comment\">#Take input from the user<\/span>\nread name\n<span class=\"hljs-comment\"># Print the value of $name with other string<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Your name is $name\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>9. <strong>How to use command-line arguments in bash?<\/strong><\/p>\n\n\n\n<p>Command-line arguments are read by $1, $2, $3\u2026$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.<\/p>\n\n\n\n<p>#!\/bin\/bash<\/p>\n\n\n\n<p>#Check any argument is provided or not<\/p>\n\n\n\n<p>if [[ $# -eq 0 ]]; then<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; echo &#8220;No argument is given.&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; exit 0<\/p>\n\n\n\n<p>fi<\/p>\n\n\n\n<p>#Store the first argument value<\/p>\n\n\n\n<p>color=$1<\/p>\n\n\n\n<p># Print the argument with other string<\/p>\n\n\n\n<p>printf &#8220;You favorite color is %s\\n&#8221; $color<\/p>\n\n\n\n<p>10. <strong>What are the advantages and disadvantages of Bash Scripting?<\/strong><\/p>\n\n\n\n<p><strong>Advantages<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The syntax and structure of Bash scripts are easy to understand.<\/li>\n\n\n\n<li>There are various commands which are general and common; hence they are easy to remember.<\/li>\n\n\n\n<li>As it is an interpreted language, so compilation does not require before running.<\/li>\n\n\n\n<li>Highly efficient.<\/li>\n<\/ul>\n\n\n\n<p><strong>Disadvantages<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Input and Output performance is not that good. It is low as compared to other languages.<\/li>\n\n\n\n<li>The inspection of variables is not strict.<\/li>\n\n\n\n<li>It usually does not come pre-installed in windows, so there are chances that the code may not be compatible with the operating system.<\/li>\n\n\n\n<li>Bash does not support many functions, for example- objects, data structures, etc.<\/li>\n\n\n\n<li>Poor debugging tools.<\/li>\n<\/ul>\n\n\n\n<p>11. <strong>Explain about \u201cs\u201d permission bit in a file?<\/strong><\/p>\n\n\n\n<p>\u201cS\u201d bit is called \u201cset user id\u201d (SUID) bit.<\/p>\n\n\n\n<p>\u201cS\u201d bit on a file causes the process to have the privileges of the owner of the file during the instance of the program.<\/p>\n\n\n\n<p>For example, executing \u201cpassed\u201d command to change current password causes the user to writes its new password to shadow file even though it has \u201croot\u201d as its owner.<\/p>\n\n\n\n<p>12. <strong>Create a directory such that anyone in the group can create a file and access any person\u2019s file in it but none should be able to delete a file other than the one created by himself.<\/strong><\/p>\n\n\n\n<p>We can create the directory giving read and execute access to everyone in the group and setting its sticky bit \u201ct\u201d on as follows:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">mkdir direc1\nchmod g+wx direc1\nchmod +t direc1\n\n<\/code><\/span><\/pre>\n\n\n<p>13. <strong>How can you find out how long the system has been running?<\/strong><\/p>\n\n\n\n<p>We can find this by using the command \u201cuptime\u201d.<\/p>\n\n\n\n<p>14. <strong>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?<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">finger \u201cloginName\u201d \u2026where loginName is the login name of the\n\nuser whose information is expected.\n<\/code><\/span><\/pre>\n\n\n<p>15. <strong>What is the difference between $$ and $!?<\/strong><\/p>\n\n\n\n<p>$$ gives the process id of the currently executing process whereas $! Shows the process id of the process that recently went into the background.<\/p>\n\n\n\n<p>16. m<strong>What are zombie processes?<\/strong><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>17. <strong>How will you copy a file from one machine to other?<\/strong><\/p>\n\n\n\n<p>We can use utilities like \u201cftp,\u201d \u201cscp\u201d or \u201crsync\u201d to copy a file from one machine to other.<\/p>\n\n\n\n<p>E.g., Using ftp:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">FTP hostname\n&gt;put file1\n&gt;bye\n\nAbove copies, file file1 <span class=\"hljs-keyword\">from<\/span> the local system to destination system whose hostname is specified.\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>18. <strong>I want to monitor a continuously updating log file, what command can be used to most efficiently achieve this?<\/strong><\/p>\n\n\n\n<p>We can use tail \u2013f 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.<\/p>\n\n\n\n<p>19. <strong>I want to connect to a remote server and execute some commands, how can I achieve this?<\/strong><\/p>\n\n\n\n<p>We can use ssh to do this:<\/p>\n\n\n\n<p>ssh username@serverIP -p sshport<\/p>\n\n\n\n<p>Example<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">ssh<\/span> <span class=\"hljs-selector-tag\">root<\/span><span class=\"hljs-keyword\">@122<\/span>.52.251.171 -p <span class=\"hljs-number\">22<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Once above command is executed, you will be asked to enter the password<\/p>\n\n\n\n<p>20. <strong>I have 2 files and I want to print the records which are common to both.<\/strong><\/p>\n\n\n\n<p>We can use tail \u2013f 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.<\/p>\n\n\n\n<p>21. <strong>I want to connect to a remote server and execute some commands, how can I achieve this?<\/strong><\/p>\n\n\n\n<p>We can use ssh to do this:<\/p>\n\n\n\n<p>ssh username@serverIP -p sshport Example<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">ssh<\/span> <span class=\"hljs-selector-tag\">root<\/span><span class=\"hljs-keyword\">@122<\/span>.52.251.171 -p <span class=\"hljs-number\">22<\/span>\nOnce above command is executed, you will be asked to enter the password\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>22. <strong>I have 2 files and I want to print the records which are common to both.<\/strong><\/p>\n\n\n\n<p>We can use \u201ccomm\u201d command as follows:<\/p>\n\n\n\n<p>comm -12 file1 file2 \u2026 12 will suppress the content which are<\/p>\n\n\n\n<p>unique to 1st and 2nd file respectively.<\/p>\n\n\n\n<p>23. <strong>Write a script to print the first 10 elements of Fibonacci series.<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/sh<\/span>\na=<span class=\"hljs-number\">1<\/span>\nb=<span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $a\n<span class=\"hljs-keyword\">echo<\/span> $b\n<span class=\"hljs-keyword\">for<\/span> I in <span class=\"hljs-number\">1<\/span> <span class=\"hljs-number\">2<\/span> <span class=\"hljs-number\">3<\/span> <span class=\"hljs-number\">4<\/span> <span class=\"hljs-number\">5<\/span> <span class=\"hljs-number\">6<\/span> <span class=\"hljs-number\">7<\/span> <span class=\"hljs-number\">8<\/span>\n<span class=\"hljs-keyword\">do<\/span>\nc=a\nb=$a\nb=$(($a+$c))\n<span class=\"hljs-keyword\">echo<\/span> $b\ndone\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>24. <strong>How will you connect to a database server from Linux?<\/strong><\/p>\n\n\n\n<p>We can use isql utility that comes with open client driver as follows:<\/p>\n\n\n\n<p>isql \u2013S serverName \u2013U username \u2013P password<\/p>\n\n\n\n<p>25. <strong>What are the 3 standard streams in Linux?<\/strong><\/p>\n\n\n\n<p>0 \u2013 Standard Input1 \u2013 Standard Output2 \u2013 Standard Error<\/p>\n\n\n\n<p>26. <strong>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?<\/strong><\/p>\n\n\n\n<p>command &lt;file1 1&gt;file2 2&gt;file3<\/p>\n\n\n\n<p>27. <strong>What will happen to my current process when I execute a command using exec?<\/strong><\/p>\n\n\n\n<p><strong>\u201c<\/strong>exec\u201d 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.<\/p>\n\n\n\n<p>E.g., Executing \u201cexec ls\u201d on command prompt will execute ls and once ls exits, the process will shut down<\/p>\n\n\n\n<p>28. <strong>How will you emulate wc \u2013l using awk?<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">awk \u2018END {<span class=\"hljs-keyword\">print<\/span> NR} fileName\u2019<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>29. <strong>Given a file find the count of lines containing the word \u201cABC\u201d.<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">grep \u2013c \u201cABC\u201d file1<\/code><\/span><\/pre>\n\n\n<p>30. <strong>What is the difference between grep and egrep?<\/strong><\/p>\n\n\n\n<p>egrep is Extended grep that supports added grep features like \u201c+\u201d (1 or more occurrence of a previous character),\u201d?\u201d(0 or 1 occurrence of a previous character) and \u201c|\u201d (alternate matching)<\/p>\n\n\n\n<p>31. <strong>How will you print the login names of all users on a system?<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">\/etc\/shadow file has all the users listed.\nawk \u2013F <span class=\"hljs-string\">':'<\/span> <span class=\"hljs-string\">'{print $1}'<\/span> \/etc\/shadow|uniq -u\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>32. <strong>How will you print the login names of all users on a system?<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">\/etc\/shadow file has all the users listed.\nawk \u2013F <span class=\"hljs-string\">':'<\/span> <span class=\"hljs-string\">'{print $1}'<\/span> \/etc\/shadow|uniq -u\nHow to <span class=\"hljs-keyword\">set<\/span> an array in Linux?\nSyntax in ksh:\nSet \u2013A arrayname= (element1 element2 \u2026.. element)\nIn bash\nA=(element1 element2 element3 \u2026. elementn)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>33. <strong>Write down the syntax of \u201cfor \u201d loop<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Syntax:\n<span class=\"hljs-keyword\">for<\/span>  iterator <span class=\"hljs-keyword\">in<\/span> (elements)\n<span class=\"hljs-keyword\">do<\/span>\nexecute commands\ndone\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>34. <strong>How will you find the total disk space used by a specific user?<\/strong><\/p>\n\n\n\n<p>du -s \/home\/user1 \u2026.where user1 is the user for whom the total disk space needs to be found.<\/p>\n\n\n\n<p>35. <strong>Write the syntax for \u201cif\u201d conditionals in Linux?<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Syntax\n<span class=\"hljs-keyword\">If<\/span>  condition is successful\nthen\nexecute commands\n<span class=\"hljs-keyword\">else<\/span>\nexecute commands\nfi\nWhat is the significance of $?\nThe command $? gives the <span class=\"hljs-keyword\">exit<\/span> status of the last command that was executed.\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>36. <strong>How do we delete all blank lines in a file?<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">sed  <span class=\"hljs-string\">'^ &#91;(backslash)011(backslash)040]*$\/d'<\/span> file1\nwhere (backslash)<span class=\"hljs-number\">011<\/span> is an octal equivalent <span class=\"hljs-keyword\">of<\/span> space and\n(backslash)<span class=\"hljs-number\">040<\/span> is an octal equivalent <span class=\"hljs-keyword\">of<\/span> the tab\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>37. <strong>How will I insert a line \u201cABCDEF\u201d at every 100th line of a file?<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">sed \u2018100i\\ABCDEF\u2019 file1<\/code><\/span><\/pre>\n\n\n<p>38. <strong>Write a command sequence to find all the files modified in less than 2 days and print the record count of each.<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">find<\/span> . \u2013<span class=\"hljs-selector-tag\">mtime<\/span> <span class=\"hljs-selector-tag\">-2<\/span> \u2013<span class=\"hljs-selector-tag\">exec<\/span> <span class=\"hljs-selector-tag\">wc<\/span> \u2013<span class=\"hljs-selector-tag\">l<\/span> {} \\;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>39. <strong>How can subroutines be declared and called in bash?<\/strong><\/p>\n\n\n\n<p>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.&nbsp; But local variables can be defined within the subroutine by using the \u2018local\u2019 keyword.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">\n<span class=\"hljs-comment\">#!\/bin\/bash<\/span>\n<span class=\"hljs-comment\"># Initialize the variable $x and $y which are global<\/span>\nx=<span class=\"hljs-number\">10<\/span>\ny=<span class=\"hljs-number\">35<\/span>\n\n<span class=\"hljs-comment\"># Declare the function<\/span>\nmyFunc () {\n   <span class=\"hljs-comment\"># Declare the local variable $x<\/span>\n   local x=<span class=\"hljs-number\">15<\/span>\n\n   <span class=\"hljs-comment\"># Re-assign the global variable $y<\/span>\n   y=<span class=\"hljs-number\">25<\/span>\n\n   <span class=\"hljs-comment\"># Calculate the sum of $x and $y<\/span>\n   z=$((x+y))\n\n   <span class=\"hljs-comment\"># Print the sum of a local variable, $x, and global variable, $y<\/span>\n   <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"The sum of $x and $y equal to $z\"<\/span>\n}\n\n<span class=\"hljs-comment\"># Call the function<\/span>\nmyFunc\n\n<span class=\"hljs-comment\"># Print the sum of global variables, $x, and $y<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"The sum of $x and $y equal to $z\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>40. <strong>How to cut and print some part of a string data in bash?<\/strong><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash<\/span>\n<span class=\"hljs-comment\"># Initialize a string value into $string<\/span>\nstring=<span class=\"hljs-string\">\"Python Scripting Language\"<\/span>\n<span class=\"hljs-comment\"># Cut the string value from the position 7 to the end of the string<\/span>\n<span class=\"hljs-keyword\">echo<\/span> ${string:<span class=\"hljs-number\">7<\/span>}\n<span class=\"hljs-comment\"># Cut the string value of 9 characters from the position 7<\/span>\n<span class=\"hljs-keyword\">echo<\/span> ${string:<span class=\"hljs-number\">7<\/span>:<span class=\"hljs-number\">9<\/span>}\n<span class=\"hljs-comment\"># Cut the string value from 17 to 20<\/span>\n<span class=\"hljs-keyword\">echo<\/span> ${string:<span class=\"hljs-number\">17<\/span>:<span class=\"hljs-number\">-4<\/span>}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>41. <strong>Mention some ways to perform arithmetic operations in bash?<\/strong><\/p>\n\n\n\n<p>Arithmetic operations can be done in multiple ways in bash. \u2018let\u2019, \u2018expr\u2019, \u2018bc\u2019 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.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash                                                                      <\/span>\n<span class=\"hljs-comment\"># Calculating the subtraction by using expr and parameter expansion<\/span>\nvar1=$( expr <span class=\"hljs-number\">120<\/span> - <span class=\"hljs-number\">100<\/span> )\n<span class=\"hljs-comment\"># print the result<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $var1\n<span class=\"hljs-comment\"># Calculate the addition by using let command<\/span>\nlet var2=<span class=\"hljs-number\">200<\/span>+<span class=\"hljs-number\">300<\/span>\n<span class=\"hljs-comment\"># Print the rsult<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $var2\n<span class=\"hljs-comment\"># Calculate and print the value of division using \u2018bc\u2019 to get the result<\/span>\n<span class=\"hljs-comment\"># with fractional value<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"scale=2; 44\/7\"<\/span> | bc\n<span class=\"hljs-comment\"># Calculate the value of multiplication using double brackets<\/span>\nvar3=$(( <span class=\"hljs-number\">5<\/span>*<span class=\"hljs-number\">3<\/span> ))\n<span class=\"hljs-comment\"># Print the result<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $var3\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>42. <strong>How to check a directory exists or not using bash?<\/strong><\/p>\n\n\n\n<p>Bash has many test commands to check if a file or directory exists or not and the type of the file. \u2018-d\u2019 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.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash                                                                  <\/span>\n<span class=\"hljs-comment\"># Assign the directory with path in the variable, $path<\/span>\npath=<span class=\"hljs-string\">\"\/home\/ubuntu\/temp\"<\/span>\n<span class=\"hljs-comment\"># Check the directory exists or not<\/span>\n<span class=\"hljs-keyword\">if<\/span> &#91; -d <span class=\"hljs-string\">\"$path\"<\/span> ]; then\n   <span class=\"hljs-comment\"># Print message if the directory exists<\/span>\n   <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Directory exists\"<\/span>\n<span class=\"hljs-keyword\">else<\/span>\n   <span class=\"hljs-comment\"># Print message if the directory doesn\u2019t exist<\/span>\n   <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Directory not exists\"<\/span>\nfi\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>43. <strong>How can a bash script be terminated without executing all statements?<\/strong><\/p>\n\n\n\n<p>Using \u2018exit\u2019 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.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash<\/span>\n\n<span class=\"hljs-comment\"># Initialize the filename to the variable, $filename<\/span>\nfilename=<span class=\"hljs-string\">\"course.txt\"<\/span>\n\n<span class=\"hljs-comment\"># Check the file exists or not by using -f option<\/span>\n<span class=\"hljs-keyword\">if<\/span> &#91; -f <span class=\"hljs-string\">\"$filename\"<\/span> ]; then\n    <span class=\"hljs-comment\"># Print message if the file exists<\/span>\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"$filename exists\"<\/span>\n<span class=\"hljs-keyword\">else<\/span>\n    <span class=\"hljs-comment\"># Print message if the file doesn't exist<\/span>\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"$filename not exists\"<\/span>\n    <span class=\"hljs-comment\"># Terminate the script<\/span>\n    <span class=\"hljs-keyword\">exit<\/span> <span class=\"hljs-number\">1<\/span>\nfi\n\n<span class=\"hljs-comment\"># Count the length of the file if the file exists<\/span>\nlength=`wc -c $filename`\n\n<span class=\"hljs-comment\"># Print the length of the file<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Total characters - $length\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>44. <strong>What are the uses of break and continue statements in bash?<\/strong><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash<\/span>\n<span class=\"hljs-comment\"># Initialize the variable $i to 0 to start the loop<\/span>\ni=<span class=\"hljs-number\">0<\/span>\n<span class=\"hljs-comment\"># the loop will iterate fot 10 times<\/span>\n<span class=\"hljs-keyword\">while<\/span> &#91; $i -le <span class=\"hljs-number\">10<\/span> ]\n<span class=\"hljs-keyword\">do<\/span>\n    <span class=\"hljs-comment\"># Increment the value $i by 1<\/span>\n    (( i++ ))\n    <span class=\"hljs-comment\"># If the value of $i equal to 8 then terminate the loop by using 'break' statement<\/span>\n    <span class=\"hljs-keyword\">if<\/span> &#91; $i -eq <span class=\"hljs-number\">8<\/span> ]; then\n         <span class=\"hljs-keyword\">break<\/span>;\n    fi\n    <span class=\"hljs-comment\"># If the value of $i is greater than 6 then omit the last statement of the loop<\/span>\n    <span class=\"hljs-comment\">#  by using continue statement<\/span>\n    <span class=\"hljs-keyword\">if<\/span> &#91; $i -ge <span class=\"hljs-number\">6<\/span> ]; then\n         <span class=\"hljs-keyword\">continue<\/span>;\n    fi\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"the current value of i = $i\"<\/span>\ndone\n\n<span class=\"hljs-comment\"># Print the value of $i after terminating from the loop<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Now the value of i = $i\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>45. <strong>How to make a bash file executable?<\/strong><\/p>\n\n\n\n<p>Executable bash files can be made by using \u2018chmod\u2019 command. Executable permission can be set by using \u2018+x\u2019 in chmod command with the script filename. Bash files can be executed without the explicit \u2018bash\u2019 command after setting the execution bit for that file.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Set the execution bit<\/span>\n$ chmod +x filename.sh\n \n<span class=\"hljs-comment\"># Run the executable file<\/span>\n$ .\/filename.sh\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>46. <strong>What is meant by \u2018bc\u2019 and how can this command can be used in bash?<\/strong><\/p>\n\n\n\n<p>The full form of \u2018bc\u2019 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 \u2018expr\u2019 command. The fractional part can be rounded also by using scale value with \u2018bc\u2019 command.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash<\/span>\n<span class=\"hljs-comment\"># Calculate the division without the fractional value<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"39\/7\"<\/span> | bc\n \n<span class=\"hljs-comment\"># Calculate the division with the full fractional value<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"39\/7\"<\/span> | bc -l\n \n<span class=\"hljs-comment\"># Calculate the division with three digits after the decimal point<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"scale=3; 39\/7\"<\/span> | bc\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>47. <strong>How can you print a particular line of a file in bash?<\/strong><\/p>\n\n\n\n<p>There are many ways to print a particular line in bash. How the \u2018awk\u2019, \u2018sed\u2019 and \u2018tail\u2019 commands can be used to print a particular line of a file in bash is shown in the following example.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash                                                                <\/span>\n\n<span class=\"hljs-comment\"># Read and store the first line from the file by using `awk` command with NR variable<\/span>\nline1=`awk <span class=\"hljs-string\">'{if(NR==1) print $0}'<\/span> course.txt`\n<span class=\"hljs-comment\"># Print the line<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $line1\n\n<span class=\"hljs-comment\"># Read the second line from the file by using `sed` command with -n option<\/span>\nline2=`sed -n <span class=\"hljs-number\">2<\/span>p course.txt`\n<span class=\"hljs-comment\"># Print the line<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $line2\n                         \n<span class=\"hljs-comment\"># Read the last line from the file by using `tail` command with -n option<\/span>\nline3=`tail -n <span class=\"hljs-number\">1<\/span> course.txt`\n<span class=\"hljs-comment\"># Print the file<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $line3\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>48. <strong>What is IFS?<\/strong><\/p>\n\n\n\n<p>IFS is a special shell variable. The full form of IFS is Internal Field Separator,<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash<\/span>\n<span class=\"hljs-comment\"># Declare ':' as delimiter for splitting the text<\/span>\nIFS=<span class=\"hljs-string\">\":\"<\/span>\n<span class=\"hljs-comment\"># Assign text data with ':' to $text<\/span>\ntext=<span class=\"hljs-string\">\"Red:Green:Blue\"<\/span>\n<span class=\"hljs-comment\"># for loop will read each word after splitting the text based on IFS<\/span>\n<span class=\"hljs-keyword\">for<\/span> val in $text; <span class=\"hljs-keyword\">do<\/span>\n    <span class=\"hljs-comment\"># Print the word<\/span>\n    <span class=\"hljs-keyword\">echo<\/span> $val\ndone\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>49. <strong>How to find out the length of a string data?<\/strong><\/p>\n\n\n\n<p>\u2018expr\u2019, \u2018wc\u2019 and \u2018awk\u2019 commands can be used to find out the length of a string data in bash. \u2018expr\u2019 and \u2018awk\u2019 commands use length option, \u2018wc\u2019 command uses \u2018\u2013c\u2019 option to count the length of the string.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>The uses of the above commands are shown in the following script.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash                      <\/span>\n<span class=\"hljs-comment\"># Count length using `expr` length option<\/span>\n<span class=\"hljs-keyword\">echo<\/span> `expr length <span class=\"hljs-string\">\"I like PHP\"<\/span>`\n<span class=\"hljs-comment\"># Count length using `wc` command<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"I like Bash\"<\/span> | wc -c\n<span class=\"hljs-comment\"># Count length using `awk` command<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"I like Python\"<\/span> | awk <span class=\"hljs-string\">'{print length}'<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>50. <strong>How to run multiple bash script in parallel?<\/strong><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Assign a folder name with the path in the variable $dir that contains<\/span>\n<span class=\"hljs-comment\"># multiple bash files<\/span>\ndir=<span class=\"hljs-string\">\"home\/Ubuntu\/temp\"<\/span>\n\n<span class=\"hljs-comment\"># for loop will read each file from the directory and execute in parallel<\/span>\n<span class=\"hljs-keyword\">for<\/span> script in dir<span class=\"hljs-comment\">\/*.sh\ndo\n    nohup bash \"$script\" &amp;\ndone\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>51. <strong>How can conditional statements be used in bash?<\/strong> 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. \u2018If\u2019 statement can be declared in two ways in a bash script and every type of \u2018if\u2019 block must be closed with \u2018fi\u2019. \u2018if\u2019 statement can be defined by third brackets or first brackets like other programming languages<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">Syntax<\/span>:\n<span class=\"hljs-selector-tag\">A<\/span>.\n\n<span class=\"hljs-selector-tag\">if<\/span> <span class=\"hljs-selector-attr\">&#91; condition ]<\/span>;\n<span class=\"hljs-selector-tag\">then<\/span>\n<span class=\"hljs-selector-tag\">statements<\/span>\n<span class=\"hljs-selector-tag\">fi<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">B.\n\nif &#91; condition ]; then\nstatements <span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-keyword\">else<\/span>\nstatement <span class=\"hljs-number\">2<\/span>\nfi\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">C<\/span>.\n\n<span class=\"hljs-selector-tag\">if<\/span> <span class=\"hljs-selector-attr\">&#91; condition ]<\/span>; <span class=\"hljs-selector-tag\">then<\/span>\n<span class=\"hljs-selector-tag\">statement<\/span> 1\n<span class=\"hljs-selector-tag\">elif<\/span> <span class=\"hljs-selector-attr\">&#91; condition ]<\/span>; <span class=\"hljs-selector-tag\">then<\/span>\n<span class=\"hljs-selector-tag\">statement<\/span> 2\n\u2026.\n<span class=\"hljs-selector-tag\">else<\/span>\n<span class=\"hljs-selector-tag\">statement<\/span> <span class=\"hljs-selector-tag\">n<\/span>\n<span class=\"hljs-selector-tag\">fi<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash<\/span>\n\n<span class=\"hljs-comment\"># Assign a value to $n<\/span>\nn=<span class=\"hljs-number\">30<\/span>\n<span class=\"hljs-comment\"># Check $n is greater than 100 or not<\/span>\n<span class=\"hljs-keyword\">if<\/span> &#91; $n -gt <span class=\"hljs-number\">100<\/span> ]; then\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"$n is less than 100\"<\/span>\n<span class=\"hljs-comment\"># Check $n id greater than 50 or not<\/span>\nelif &#91; $n -gt <span class=\"hljs-number\">50<\/span> ]; then\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"$n is less than 50\"<\/span>\n<span class=\"hljs-keyword\">else<\/span>\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"$n is less than 50\"<\/span>\nfi\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>This article has all the interview questions and answers of Bash scripting. All the possible FAQ&#8217;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.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash Scripting is a powerful a part of system administration and development used at an extreme level. It\u2019s 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. Script:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":26445,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[18],"tags":[641,2568,2570,7182,7183,5449,766,2497,2504,3347,139],"class_list":["post-26426","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-batch-scripting","tag-answers","tag-bash-aliases-for-git","tag-bash-git-aliases","tag-bash-script","tag-bash-script-tutorial","tag-devopsschool","tag-interview","tag-interview-questions","tag-interview-questions-answer","tag-interview-questions-answers","tag-scripting"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/26426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=26426"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/26426\/revisions"}],"predecessor-version":[{"id":32356,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/26426\/revisions\/32356"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/26445"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=26426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=26426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=26426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}