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.

______________________

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix