Tag Archive: Unix



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      

Types Of Shells


Shells plays an important role in Linux and Unix system. Everything that is happening on your distro is either because of the shell or the kernel. Moreover, a backend  shell is initialized as soon as you made a login . There are lots of shells in the Unix and Linux system. Let’s try to know more about them :

What is a shell ?

Shell is the software installed on your system that works as command-line interpreter i.e. it takes the commands you type, interpret them and gives them to operating system (kernel) through system calls to execute. There are many different shells available to choose from in Linux.

C Shell : C shell was created by Bill Joy, while he was studying at University of California, Berkeley. He released it under BSD license, the syntax of C shell is similar to C programming language hence the name C shell.

TC Shell: An improved version of C shell, provides some additional features like command line completion and editing. In most of Linux systems /bin/csh is linked to /bin/tcsh and it alternatives, so when you try to start the C shell, TC shell gets started.

C shell linked to its alternatives

Korn Shell (ksh): Korn shell was developed by David Korn at AT&T Bell Laboratories. It contains may features of C shell and Bourne shell.

Bourne Shell(sh): The default shell in many Unix systems developed by Stephen Bourne of AT&T Bell Laboratories.

GNU Bourne-Again Shell (bash): Bash shell is default shell in most of Linux distributions. Its a free and open source clone of bourne shell. It was developed by Brian Fox for the GNU project. Its the most feature rich shell available and is compatible with bourne shell.

To find out all the shells installed on your system type “$cat /etc/shells”. Your default shell is defined /etc/passwd file.

My Default Shell

To switch to a different shell on your system just type the name of that shell (as in /etc/shells) and hit enter.

Z Shell(zsh):  Sorry I forgot to write about this shell (Don’t Know How :( ). Thanks to all of you for reminding me. Well The Z shell (zsh) is a Unix shell that can be used as an interactive login shell and as a powerful command interpreter for shell scripting. Zsh can be thought of as an extended Bourne shell with a large number of improvements, including some features of bash, ksh, and tcsh. It includes features like automated spell correction, editing  multiple line command within a single buffer and many more.

In the next part of the article we will learn about “How bash shell interprets a command”. So Google about the shells more and more. If want to become a Linux administrator, then you must  have  a good control over the shells.

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 .

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      


I am reading this book – “Unix concepts and applications” written by the great author “Sumitabha Das”. This book is very good to start from the basics. If you wanna buy it , you can purchase it from Buy Unix Concepts And Applications from Flipkart.com .

It is very difficult to remember each shell command, even experts get confused with them sometimes. There’s an inbuilt documentation called as Man pages in Unix/Linux which practically provides the documentation of every command. You just have to type “man command” (without Quotes). What man command does,it searches for the documentation of the given command and  display it on the shell through the pager program. Man documentation has been divided into several sections. As of present it is divided into 9 sections each having a collection of specific category. Sections are-

1. Executable programs or shell commands

2. System Calls

3. Library Calls

4. Special Files (Usually Found in /dev)

5.File Formats and Coventations

6. Games

7. Miscellaneous

8. System Admin Commands

9. Kernel Routines

Most of the commands are available in the section 1. When you call the man documentation such as   “man wc”  man will look for the documentation of the wc command in the sections starting from section number 1. As soon as it finds it, it will stop looking further and  will display the documentation of the wc from the section 1.  There are many commands which are available in the multiple sections of the man documentation. For example passwd command is available in both section 1 and section 4. To locate the documetation section wise you should type the section number as an argument. Ex- “man 4 passwd” . This will display the man page of passwd form the section 4 not from the section 1.

Pages : Follow ankurtwi on Twitter      


If you are interested in knowing how an operating system works this book “Operating systems” By “Gary Nutt” is a great source of reading. I just started reading it and already got impressed with the way things are presented in the book. It covers all the topics from system tools to applications tools. Moreover it covers many of the platforms of the present and the past like Unix,Linux,BSD,windows,MacOsX. A must read for all :) I searched the web for ebbok but unable to find it so,I bought it through flipkart. Here’s the link of the same Buy Operating Systems 3rd Ed. from Flipkart.com

I would also like to recommend you guys this great book which not only motivates you but also teaches you important aspects of the Technical world. If you are passionate about Google like me you must read it :D:D Buy The Google Story from Flipkart.com.....

I am reading about Pygame a lot these days. Was looking for a good book and found this one :) It’s simply great :) Buy Game Programming: The L Line 1st Edition from Flipkart.com.

And if you are crazy enough about the design patterns, This book will be your best partner  Design Patterns: Elements Of Reusable Object-Oriented Software....

Pages : Follow ankurtwi on Twitter      

BSD Day


The Berkeley Software Distribution (BSD) is a free open source version of the Unix operating system which evolved at the University of Berkeley starting from 1975, and it is based on AT&T’s Unix Sixth Edition (V6). The name BSD is now used collectively for the modern descendants of these distributions. Most notable among these today is perhaps the major open source BSDs (FreeBSDNetBSDOpenBSD) which have themselves spawned a number of children.

Read the full story at  http://wiki.freebsd.org/BSDDay_

 

Pages : Follow ankurtwi on Twitter      

Follow

Get every new post delivered to your Inbox.

Join 70 other followers

%d bloggers like this: