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
Fabric-Based Computing Enables Optimized Hyperscale Data Centers

Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.

Learn More

Sponsored by AMD

White Paper
Red Hat White Paper: Using an Open Source Framework to Catch the Bad Guy

Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6

Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.

Learn more about catching the bad guy in this free white paper.

Learn More

Sponsored by DLT Solutions