Bash Frequently Asked Questions

What is Bash

Bash is a command line interpreter for the GNU operating system. Bash is acronym for Borne again shell. In Unix and Linux, Bash is default command line shell interpreter. There are other shells which one can enable such as zsh, csh and fish shell.

What is the use of bin bash

!/bin/bash is first line put at the beginning of file to let interpreter know that it is a bash script. #!/bin/bash is also called bash shebang.

Similarly if it is csh script. We can put #!/bin/csh at the beginning of file for c shell script

What is bash programming language

Although bash is a command line interpreter, it is also a programming language. Bash as programming language is a very powerful tool. We can automate whole bunch of tasks using bash.

What does -n mean in bash

-n is used to test "is not empty". We can use -n to check if variable is empty.


Lets check if x is empty or not.

if [ -n "$x" ];then
    echo "not empty"
    echo "empty"

As expected we got the $x as not empty.

Lets check the variable $z which we have not declared and run the same code above. We should get the result as empty.

if [ -n "$z" ];then
    echo "not empty"
    echo "empty"

What is $? In shell script

$? stores the exit value of the last command used on the prompt.

Lets do an example. Lets initialize the value to a variable.


Lets check the exit status of above command

echo $?

$? exit status is 0

What is $# in bash script

$# contains the number of arguments that were passed to a command. Lets do an example by creating a script which takes one argument.

echo '#!/bin/bash' > /tmp/
echo 'echo $1' >> /tmp/

echo 'echo $#' >> /tmp/

cat /tmp/

echo $1
echo $#

bash /tmp/ "hello world"
hello world

As we see above, "$#" gives number of arguments which is one in our example above.

What is difference between sh and bash

Bash is acronym for bourne again shell. It is the newer version of the original shell sh.

How do I run a bash script

First add the #!/bin/sh as the first line in your script assuming your shell executable is in /bin directory

Secondly, add your code after the first line.

Save the file as

Make the file executable by doing

chmod +x

Related Topics:

How To Check If String Is Null Or Not Using -z or -n Switch In Bash

Autocomplete Command History In Bash

How To Use For Loop In Bash

How To Write Functions In Bash