Tag Archive: PATH (variable)



In this previous part of this article we learned about  types of shells . In most of the Linux distributions , bash shell is the default shell. So we are going to discuss the working of bash shell in this article . Fasten your seat belts and get ready for the bash ride :D

Normally we think that when we type a command, shell looks for the command in all the directories defined in the PATH environment variable. But in real it goes through the following sequence to reach the above step.

  1. Redirection
  2. Aliases
  3. Expansion
  4. Shell Function
  5. Shell Builtin
  6. Hash table
  7. PATH variable

Now lets first discuss them one by one:

1. Redirection: Consider the example given in the following snapshot:     

Redirection

Here files file1, file2, file3 exists in the current directory as seen from the output of ls command but when we redirect the output to out.txt file, the out.txt file should contain the output of ls command i.e. file1 file2 file3 but in real it also includes out.txt. This happens because before ls is executed redirection is done and file out.txt is created in current directory (we can’t redirect output to a file that doesn’t exit).

2. Aliases: After redirection shell moves to aliases.

Aliases

Here I have defined alias ls=cat , now when I type ls out.txt , ls is replaced with cat and contents of out.txt file is displayes. To make the shell ignore alias use , precede the command name with backslash (“\”).

3. Expansion: According to bash manpage there are seven kind of expansions : brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, word splitting, and pathname expansion. Discussing all of them is beyond the scope of this article, refer to bash manpage (man bash) for more information. If the command you typed contain any variable name, wildcards (*,? etc) shell expands them. As an example refer to the screenshot below :

Expansion

Here $HOME becomes /home/sumit (my home directory) after expansion and file* is expanded to file1, file2, and file3.

4. Shell Function: Like many programming languages bash allows you to define functions. A function in bash may contain multiple commands. You can execute a function just by typing its name. Example : Look  in the figure below, here I have defined an alias by name “ls” and a function by the same name. Since alias gets priority over function, when I first type ls and press enter /bin/ls is executed. Next I have used backspace to ignore alias. After deleting alias using unalias command, since there is no alias left ls corresponds to function ls. 

Shell Function

5. Shell Builtin : Some commands are a part of bash code itself, they are called Shell builtin commands. They are given priority over hash table and directories in PATH variable. Example : echo is a shell builtin command, you can use type command to determine how a given keyboard will be interpreted. 

Shell Builtin

Here its clear that echo is shell builtin command, echo also exist in /bin/ folder but if you type echo and hit enter , it will be ignored since builtin command are  of more  priority. Notice how the output of “type ls” changes after defining the alias by the same name. Once we delete alias and function ls, bash searched hash table, since ls is not in hash table is look in PATH and its found in /bin/ls. 

6. Hash table: The concept of hash table is similar to cashing in Linux, shell store the full path of all the executed commands to speed things up. Now in the snapshot below I have started a new shell, after that I execute ls command (since there is no alias or function by the same name, it executes /bin/ls because /bin is in PATH). Then after executing echo and firefox, I have used hash command to look at hash table. The first column “hits” display cache hits and “command” column displays full path of the command. Because  echo is internal command its not displayed in hash table. After I execute ls one more time its hit column changes from 1 to 2.

Hash Table

7. PATH variable: Last of all shell searches for a given command in directories listed in PATH environment variable. You can look at the contents of PATH by typing

$echo $PATH

To add any directory to PATH variable use

$export PATH=$PATH:/path/to/dir

where /path/to/dir is the absolute path to the directory you want to add. For more information you can read bash manage ($man bash).     Also to make the changed PATH settings permanent add the line export PATH=$PATH:/path/to/dir to .bashrc file in your home directory.

$echo “PATH=$PATH:/path/to/dir” >> /home/your_user_name/.bashrc

Note: Make sure you use >> instead of >, otherwise .bashrc file will get overwritten instead of appending.

About Author : This post is written by Sumit Rai. He is second in command of this blog. Sumit loves to play with grub and spends most of his time in manipulating the hardware through shell.  He is trying his hands on assembly languages also. You can contact him at sumitrai96@gmail.com .

If you want to distribute your knowledge of open-source or Linux in any form like we are doing , feel free to contact us . We can do this through this ad-free blog. We think this is the only way we can repay a little to the work of GOD RICHARD STALLMAN and LINUS TORVALDS .

Pages : Follow ankurtwi on Twitter      


Cat is one of the most well know commands of the Linux as well as of Unix systems. It’s mainly use to display the file contents of a small file on the terminal. But scope of the cat commands is not just limited to  display, We can use the cat command to create,append and even in the concatenation of the two files. In this post we will learn how to complete these jobs using the same cat command.

Display : As stated above we mostly use cat utility for the display purpose only. To display a file using cat use the following syntax without brackets “cat filename1 filename2…” . You can display single file as well as multiple files in one go. In case of multiple files, file2 will get display after the file1. Example – In the screenshot below we are displaying the contents of the test file using cat command.

Display Using Cat

You can use options like ‘-v’ , ‘-n’ etc to enrich the display. Look at the man pages for the options by typing man cat.

Create This is the best part about cat. You can even create your text files using this utility. Enter the cat command followed by the redirect output ‘>’ character and the filename. In short, something like this “cat > file” . When you press enter  the prompt vanishes. cat now waits to take the input from the user. Enter your text. Press [Ctrl-d] to signify the end of file character.The system will understand that no further text input will be made. The file is written and the prompt is returned. To verify you can use the                    cat file again.

Append : We can append a file by using append character “>>” utility along with the cat command.  Syntax for appending  is “cat >> file”. In this cat waits for the user to enter the input on to the terminal and then reads it. After reading it appends it into the file.

Append Your Files

Concatenate : Sometimes you need to concatenate two files especially if you are following the modular coding techniques. You can easily concatenate two files by using the redirecting output parameter ‘>’.  We can concatenate two files into the third file by following a syntax like “cat file1 file2 >file3″ . It will first write down the file1 and then file2 into the file3.

Scope of cat is not just limited to only these four functions. You can learn more about this utility using the web or by reading the man pages.

Pages : Follow ankurtwi on Twitter      


We can classify all of the Linux/Unix commands into two types- Internal And External. Internal commands are the shell built-in commands while external commands are  files present in the $PATH. (Remember we treat everything in the Linux as a file). Commands like pwd, cd ,echo comes under the category of the  internal commands while ls,cp etc comes in the other category. To check whether the command is internal  or external we will use the typeutility (type itself is internal one). Just type the name of the command after the utility and it will return you something like this -

Using Type Utility

If you got a message like “cd is a shell builtin” after the execution of the command  it is an internal command. If you got something else it comes under the category of external commands (like cp in this case).

How Execution  Takes Place : For the external commands, execution is possible  only if the command file is present in the locations mentioned in the $PATH. Otherwise it will give you an error.If the command file is available  , but the path to that file is not included in the PATH variable, it will show you an error. Internal commands don’t need files for their execution. Set of internal commands is stored in shell by default. Sometimes a command with the same name exist in both the categories. Example – echo (external one lies in the /bin/echo) . In this case shell will give preference to the  internal echo first, then comes the turn of the external ones.  So if you use the echo utility internal will get executed. External echo rarely gets executed (You can execute it in the shell  by creating a subshell though).

Pages : Follow ankurtwi on Twitter      

Follow

Get every new post delivered to your Inbox.

Join 69 other followers

%d bloggers like this: