More BASH Fun

You all might get tired of my continually sharing my enthusiasm for the BASH (Bourne Again SHell) command interpreter, but I truly believe you won't unleash the true power of Unix until you become fluent with your shell. File managers are fine for what they are, but in time, you will evolve beyond the need for them. When you become truly proficient with a command shell, a graphical file manager will simply get in your way.

Refresher. The three primary areas of your $HOME/.bashrc file are aliases, variables, and functions. Each time you start a new bash shell, you inherit these aliases, variables, and functions for all processes you launch from this new shell. Therefore, if you find ways of making life easier and incorporate those methods into your default shell environment, you save yourself quite a bit of time in the long run.

Other significant resource files in your home directory are $HOME/.bash_profile, $HOME/.bash_logout, and $HOME/.bash_history. The first file only gets evaluated once, at the start of every login session. The next gets evaluated only when you exit a session, such as clearning a screen or displaying how long you were logged in, or whatever. And the history file contains a history of your commands from the command line. I'm just going to talk about the $HOME/.bashrc file for now, because that's the real workhorse.

Making it $HOME. I recommend rewriting your .bashrc file from scratch, or at least commenting it up and reformatting it so that you aren't looking through someone else's half-baked mess. Take charge and make your shell a nice place to work. Since you'll be in and out of your .bashrc frequently while you're setting it up, don't be afraid to try different things that might work for you.

Here are some pointers:

Fun Experiments. After you get comfortable with the basics, you'll want to try some different things. Here are a few ideas.

PS1 Variable. Also known as your shell prompt variable, this governs how your command line appears. You can get very simple and minimalistic with it, or you can get very elaborate with it. Normally, the default for the system is in /etc/profile. Often the value is something like PS1="[\u\@\h \W]\$ ", which would equate to something like [you@yourhost currentdir] $ .

This is fine for most people, but you might want to try out something like PS1=" \d \@ BASH \V UID: $UID Cmd: \# Hist: \! \n [\u\@\h \W]\$ ". It will produce a command prompt something like Fri Apr 9 04:10pm BASH 2.01.1 UID: 1000 Cmd: 7 Hist: 506

[you@yourhost currdir]$

You can even insert escape codes that will change the ANSI color sequence for your command prompt. Take a look at the BASH manual page where it talks about ``PROMPTING''. It's very helpful.

Nice Functions. Functions are where you can really do cool things. To give you an idea of what's possible, let's take the ``PS1'' variable example from above to give us a small prompt theme function. This way, we can change how our bash prompt appears dynamically. Simply name the function, followed by closed parents, and enclose the function in brackets, as below. This function is called ``bp'', for bash prompt.

bp()
{
clear; echo -e "\nYour prompt themes are default, plain, fancy.\n\n"
read bpchoice
case "$bpchoice" in
  default)
    . /etc/profile      # use system default
  ;;
  plain)
    export PS1="[\u@\h \W]\$ "    # [you@yourhost basedir]$ 
    export PS2="> "               # for subshells
  ;;
  fancy)
    PS1="\[\033\[1;35m\] \$(date "+%A, %B %d %Y, %T")\n \u@\h \[\033[0m\]\$ "
    export PS1
  ;;
  *) export PS1="\W/\$ "          # minimal and simple
  ;;
esac
}

When you type ``bp'' at the shell after sourcing the new .bashrc, you get the menu produced from the echo statement. The read statement gets your answer and feeds it to the case statement. The case statement invokes the bash prompt corresponding to your choice. By the way, the fancy theme above turns your prompt blue, shows the time and date, breaks to the next line and shows "you@yourhost $" again. Then it turns the color back to the default you had before. You can get quite a lot more sophisticated than this, but this will give you a start.

For more ideas, take a look at the bashprompt utility available at http://bash.current.nu.

Conclusion. Once you get used to adding your brainstorms and bright ideas to your environment, you'll feel like a true Unix Geezer, and all your friends will be impressed.



David S. Jackson
Sat Apr 17 14:47:52 EDT 1999