≡ Menu

bash shell

nixCraft FAQ Roundup June 19, 2007

Recently updated/posted Linux and UNIX FAQ (mostly useful to Linux/UNIX new administrators or users) :

HowTo: Add Pause Prompt In a Shell Script ( bash pause command )

Most of you may be aware of old good DOS/2000/XP pause command. It is used display the prompt while suspending processing of a batch script. It is used within a computer batch file and allows the computer to pause the currently running batch file until the user presses any key.
[click to continue…]

HowTo: Debug a Shell Script Under Linux or UNIX

Raju asks:

How can I Debug a shell scripts?

This is most common question asked by new admins or UNIX user.

Shell scripting debugging can be boring job (read as not easy). There are various ways to debug a shell script.
[click to continue…]

nixCraft FAQ Roundup Oct 29, 2008

Couple of quick question answered by me:

=> Host a domain without CPanel or Plesk Control Panel

=> Bash shell display only hidden (dot) files

=> Monitor Linux user activity in real time

=> Apache name based VirtualHost example

=> How do I find out my mail server blacklisted?

=> Solaris find out a package which a file belongs to

=> MySQL server status with mysqlreport report script

=> Exclude certain files when creating a tarball using tar command


Linux/UNIX: Find Out If File Exists With Conditional Expressions

With the help of BASH shell and IF command it is possible to find out if file exists or not. Generally, this is known as conditional expressions. Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons. General syntax:

[ parameter FILE ]
test parameter FILE

Where parameter can be any one of the following:

  • -e: Returns true value if file exists
  • -f: Return true value if file exists and regular file
  • -r: Return true value if file exists and is readable
  • -w: Return true value if file exists and is writable
  • -x: Return true value if file exists and is executable
  • -d: Return true value if exists and is a directory


Find out if file /etc/passwd file exists or not

Type the following commands:
$ [ -f /etc/passwd ] && echo "File exists" || echo "File does not exists"
$ [ -f /tmp/fileonetwo ] && echo "File exists" || echo "File does not exists"

Find out if directory /var/logs exists or not

Type the following commands:
$ [ -d /var/logs ] && echo "Directory exists" || echo "Directory does not exists"
$ [ -d /dumper/fack ] && echo "Directory exists" || echo "Directory does not exists"

You can use conditional expressions in a shell script:

if [ -f $FILE ];
   echo "File $FILE exists"
   echo "File $FILE does not exists"

Save and execute the script:
$ chmod +x script.sh
$ ./script.sh /path/to/file
$ ./script.sh /etc/resolv.conf

You can use this technique to verify that backup directory and backup source directory exits See example script for more information.

How BASH Shell Command Search Sequence Works

This article was organically contributed by monk.

HASH tables and PATH is not the first method locating your program or executable files on a Linux or Unix-like systems. Your program can be a shell function or builtin command or an alias. Here is the complete sequence adopted by BASH shell to execute your command:

  1. Before a command is executed REDIRECTION is done. Then following sequence used by SHELL
  3. Parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable
  4. Shell FUNCTION
  5. BUILTIN command
  6. HASH tables
  7. PATH variable
  8. If everything fails, you see command not found error message.


Try out the following simple examples to understand the SHELL sequence

a) Create your own bin directory and add to a PATH:
$ mkdir ~/bin
$ export PATH=$PATH:~/bin

b) Create a shell function called hello:
$ function hello() { echo "Hello from function()" ; }

c) Create an alias called hello:
$ alias hello='echo Hello from alias'

d) Create a shell script called hello in /home/you/bin directory (which is added to your PATH)
$ echo 'echo Hello from script' > ~/bin/hello
$ chmod +x ~/bin/hello

e) Let us test it:
Now, you have three different commands with the same name (i.e. hello). Which one will execute first? Type the following commands at command prompt:
$ type hello
$ hello

Sample outputs:

Hello from alias

f) Remove an alias and run hello again:
$ unalias hello
$ hello

Sample outputs:

Hello from function()

g) Remove a shell function hello and run hello again:
$ unset hello
Sample outputs:

Hello for script

h) Remove a shell script hello and run hello again:
$ rm ~/bin/hello
$ hello

Sample outputs:

bash: /home/monk/bin/hello: No such file or directory

You just removed the hello script but Bash shell still looking hello program at /home/monk/bin/hello location. What is going on here? The answer is pretty simple, "HASH table. The shell is looking for a cached PATH entry for hello script. To verify this just type the hash command to see HASH table:
$ hash
Sample outputs:

hits    command
1    /bin/rm
1    /bin/cat
2    /usr/bin/print
3    /home/monk/bin/hello
1    /usr/bin/man

Your SHELL remembered path settings names in HASH table. There is an easy solution, just clear hash table and see the effect again:
$ hash -r
$ hello
Sample outputs:

bash: hello: command not found

I hope this basic explanation will help to all new users out there. As a result next time you run a command remember shell goes through quite a complicated sequence of operations to process your request.

Continue reading the second part of "How Linux or UNIX Understand which program to run" series (this is part I):

  • PART I : How Linux or UNIX Understand which program to run
  • PART II : An example: How shell Understand which program to run

How Linux or UNIX Understand which program to run – PART I

This article was organically contributed by monk.

When you are logged in to a Linux server and you type a command. It is the responsibility of the shell to interpret your command. Here I will explain how BASH shell finds out which program to run. The method used by SHELL is straightforward but often creates confusion for new Linux user/admins/Interns.

Remember your shell deals with different commands and command line options to process your request.
For example:

  1. Internal commands aka shell builtin command (such as set)
  2. External commands (such as clear, date)
  3. Aliases (such as alias rm='rm -i')
  4. Command substitutions ( such as echo "Today is $(date)")
  5. Functions
  6. Pipes ( such as cat /etc/passwd | wc -l)
  7. I/O redirection (such as cat /etc/passwd > /tmp/names)

As you can see, SHELL has to do many things before it can find the correct executable file for you. For example, when you type single command date; SHELL will locate date command for you. Then it spawns (forks) a new process and "execs" the date command. Please note that discussion related forks and kernel is beyond the scope of this document (see nice explanation by Tony @ How shells call other programs). Here you just want to understand how Linux knows which program to run.

Shell uses PATH variable

Your shell uses the environment variable called PATH to locate commands. Just type following command to display your current PATH:

$ echo $PATH


The variable PATH defines search path for commands. As you can see, PATH holds a colon-separated list of directories in which the shell looks for commands. Returning to the date example, when you type date command, shell will start with the directory on left (i.e. /usr/local/bin) side of PATH variable and checks to see if there is date command executable file. If executable file found, shell will execute date command. If command cannot be located at all in all directories then you will see command not found error message. BASH shell use following sequence to execute command (for example purpose, we will use date command):

  1. If there exists a shell FUNCTION date() execute it and stop.
  2. If there exists a shell builtin date command, execute it and stop
  3. If the date is neither a shell function nor a builtin then BASH searches in HASH tables. If there exists an entry for date command execute it and stop.
  4. Finally, if date does not exist in HASH tables, it will search using PATH variable.
  5. If above all method fails then SHELL will return error "Command not found" and always exit with 127 status code.

However, things started to get complicated if it is a shell script, the SHELL does exactly the same thing (as mentioned above), but the exec fails, which causes the shell to read the script and interpret it.

What is a HASH table?

A HASH table is nothing but some sort of caching mechanism to speed up things. For each command, the full file name of the command is determined by searching the directories in $PATH variable and remembered by shell in HASH table. Just type hash command and it will display the all remembered directory name:
$ hash

hits    command
5    /usr/bin/chsh
1    /usr/bin/man
1    /bin/ls

Related shell commands
To solve a command searching mysteries Linux/SHELL offers couple of commands.

type command

Tells whether command is an alias, function, buitin command or executable command file. To be frank type command indicate how it would be interpreted if used as a command name. General syntax:
type {command-name}

$ type -a ls

ls is aliased to 'ls --color=auto'

$ type date

date is hashed (/bin/date)

$ type dirs

dirs is a shell builtin

$ type if

if is a shell keyword

$ type getip

getip is a function
getip ()
lynx --dump 'http://localhost:81/getip'

which command

Use to locate a command in a PATH.
$ which ls


Continue reading the second part of "How Linux or UNIX Understand which program to run" series (this is part I).

  • PART I : How Linux or UNIX Understand which program to run
  • PART II : An example: How shell Understand which program to run

Updated for accuracy by Vivek. This article almost rewritten to fix typos.