Configuring Bash

 in
A quick introduction to the Bash shell.

Welcome to the world of Bash, most widely used shell in Linux. Bash is surprisingly configurable, and, by the time you finish reading this article, you'll have an environment more comfortable for you. Bash does not differentiate between internal shell variables and external environment variables. A shell variable is a variable (usually all caps), associated with a value, and carried around between shells. Many programs use their own variables, like PILOTRATE, which they check. Bash has its own variables, like MAIL, that are important to it. Environment variables are set using the syntax:

export VAR=VALUE
or in two lines:
VAR=VALUE
export VAR
To check the value of an environment variable, type echo $VAR, or to see all set variables, type env. bash executes your ~/.bash_profile file for the login shell (on the console), and ~/.bashrc for non-login shells (xterms and the like). Often you may just want to link one to the other. If you export a variable, or set an alias on the command line, it only stays active for that one bash session. You must put it in your login script for it to stick. If you start having a monolithic .bashrc file and want better organization, you can split it up. Often, people break up their .bashrc into aliases, variables and functions, and the .bashrc simply executes the others. To have your .bashrc execute other files put in a line like this:
source FILE

The Prompt

The first environment variable we'll discuss is PS1. PS1 stores a character string that is interpreted by bash for use as your prompt. Here is a sample PS1 and its generated prompt:

PS1='<\u@\h:\w>$'
<blackmad@moomintroll:/etc>

Backslashed characters are interpreted, while other characters are displayed verbatim. \u is translated to user name, \h is translated to host name up to the first period, and \w is the working directory. Some of the most important backslashed characters, which can also be found in the bash manpage, in the PS1 section, are shown in the table “Interpreted Characters.”

Table 1. Interpreted Characters

Cool Xterm Title

One of the cool things in all X terminal emulators (xterm, rxvt, Eterm, ...) is that if you print "\033]0;STRING_HERE\007", the title of the term changes to STRING_HERE. Try it by typing:

echo -n "\033]0;Be Happy\007"

What I do with this is put a small function in my .bashrc function xtitle (see Listing 1), and I call this after I've set my PS1 variable, so at the end of my .bashrc file I have the line:

PS1=''\u@\h:\w>$''
xtitle
export PS1
This means that if I'm in a terminal emulator, it will set TITLEBAR, a string which will append user@hostname:directory to my prompt string (so it's printed each time I get a new prompt), and then export it. (Note that if your terminal emulator sets $TERM to something other then xterm* or rxvt*, you need to add another case, with | WEIRD_TERM_ENV on the line with xterm* | rxvt*) before the close parenthesis.

Listing 1. Function xtitle

Aliases

One of the most useful things to use with Bash is aliases. Aliases simply direct Bash to interpret a text string as something else. For example, you can fix it so that when you type happy, Bash interprets it as:

echo I'm a shiny happy shell

All aliases take the same form:

alias ALIAS="COMMAND"
Often, you may want to change the default behavior of a command, such as ls. I alias ls in this way:
alias ls="ls -aF --color"
ls now prints all files, in color, with classification. \ls will execute the unaliased command. Other times you may decide to define a whole new command in order to shorten the amount of repetitive typing. Here are a few aliases I use:
alias mkall=\
        "./configure && make && sudo make install"
alias whizz="ssh whizziwig@www.whizziwig.com"
alias tgz="tar -xvzf" alias ll="ls -aFl"
alias ls-d="ls -Sc"
These all save time and keystrokes, and since anything you type after the alias is still passed to Bash, it will just translate the part that is aliased. In my case, executing tgz linux-2.2.14.tar.gz actually executes tar -xvzf linux-2.2.14.tar.gz.

______________________

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState