Posted: . At: 9:33 AM. This was 3 years ago. Post ID: 14963
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How the bash shell works in Linux.


If you’ve ever piped the output of one command into the input of the other or joined simple commands together to do complex tasks, then you have a taste for the power of the shell. As you use the shell more and more you’ll discover how powerful it is and that, more often than not, the shell is quicker than the GUI. In this post, we’ll show you how to customize the Bourne Again Shell (Bash). Bash is the default shell with most distributions and by far the most popular. To customize Bash, we are going to edit two files, .bash_profile, and .bashrc, both of which are found in your home directory. If you can’t see them when you do a listing, try ls -la, and pipe it through more if the list scrolls offscreen. When you log in, .bash_profile is read and its commands executed. When you start an interactive shell that is not a login shell (say, by typing bash at the command prompt), another file .bashrc is read. Typically, .bashrc is also read into a login shell, but this is not automatic. Rather it is done explicitly by a command in the .bash_profile file. Shell variables (also called environment variables) are placeholders for information and Bash maintains a suite of these to keep track of various settings. Have a look at what shell variables Bash knows about by typing the following:

~$ env

One of those listed variables is PATH, which tells Bash in which directories to look for commands that you type. As you can see, the format of the variable is a list of directories separated by colons. When you run a command, Bash will search these directories in order to find your command. Why is this handy to know? Say you installed some new programs to /usr/local/bin — which is a common place to store user space programs that won’t get mixed up in the Linux distribution hierarchy. You might want to add this directory to your PATH so you don’t have to type /usr/local/bin/fancyprogram. You can do this as follows:

PATH=/usr/local/bin:$PATH

In fact, many distributions will add this to your user account PATHs automatically, so if you’re logged in as your standard user account you might already see this directory in your PATH. Login as root, and then run env. Chances are you won’t see /usr/local/bin in your PATH. This might explain all those times you’ve been root and found that running a program doesn’t work — when you know it works under your normal account. The default root PATH is very different from the standard user account PATH. What’s the deal with the ‘$PATH’ we hear you say. Glad you asked. Variables are referenced in the shell by their name but preceded by ‘$’ to clarify for Bash that you want to reference the variable rather than taking the words literally. So, from our example above, $PATH represents (some say ‘expands to’) the current PATH as displayed by env, to which we are adding ‘/usr/local/bin’. Yes, it’s pretty nifty that we’re giving a new value to PATH (the ‘PATH=’ part) and referencing the current value of that variable at the same time. If you find this logic exciting, you should be a programmer! So, now you know how to set a new environment variable from the command line. But how about making the change permanent? Simple, just add the line to your .bash_profile file. By the way, if you want to add a new directory to your path that is searched before other directories you just add it, of course, in front like so:

PATH=/usr/local/bin:$PATH

To initialize any changes you can exit and login again or, alternatively, you can reload it with:

. .bash_profile

An environment variable of much greater interest is the one that sets your prompt, PS1. Let’s start by setting the prompt to a string (programming lingo for ‘collection of ASCII character’s). Here’s a scary example:

PS1="C:\>"

Configured like this, PS1 is a local shell variable. If you start another shell by typing bash then the prompt in the new shell will be as it normally is. The new shell does not get a copy of this shell variable, because you haven’t told Bash to export it. If you want ‘child’ shells (scripts and the like) to inherit a variable you have to use the export command:

PS1="C:\>"
export PS1

Now if you run a new shell, the prompt will be the same as in the parent shell. Hard coding the prompt to a string of your choice may pander to your delusions — “I await your command, oh Leader >” — but it’s not very useful. Fortunately, Bash provides you with an assortment of special characters that can appear in the prompt and take on a special meaning. For example:

PS1=”\W>

The ‘\W’ value in the prompt prints the basename of the current directory. If you are currently in the directory /home/work then the prompt looks like this:

work>

If you want to see the values that are used to make up the prompt you are familiar with on your distribution, open up a new terminal (and thus a new shell with the original PS1 variable) and type this command:

env |grep PS1
Escape sequences available to bash.
Escape sequences available to bash.

Yes, grep allows us to pick out just what we want to see. We said you’d learn to love this command! Another neat trick you can do with prompts is to add color using ANSI escape sequences. An escape sequence is a little signifier that tells the interpreter (in this case, ANSI) that what follows should be interpreted and not ignored. Table 2 shows the colors available. These sequences look like gibberish, but the terminal — if it supports ANSI escape sequences — understands them. Table 1 shows that Bash allows you to embed non-printing characters into the prompt. These begin with ‘\[‘ and end with ‘\]’. Table 1 also shows that ‘\xxx’ represents the character whose octal value is ‘xxx’. The octal value 033 corresponds with the ANSI escape character and what follows are the magic incantations of Table 2 to turn on color. So, if you want your standard prompt to be purple, use the following:

export PS1=”\[\033[0;35m\]\u:\W>\[\033[0;0m\]

Remember to turn off color at the end of the prompt (‘[\033[0;0m\]’) so ‘normal services are resumed’; otherwise the prompt color will spill out on to what you type. Once you’ve experimented and settled on a prompt that displays the information you want (see Table 1) in the colors you want (Table 2), make it permanent by adding the command to your .bash_profile file.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.