bash shell

Colour

To enable color (syntax) highlighting in the terminal window, start by checking ~/.bashrc - it will often contain good settings which can just be un-commented.

Commands

alias

This is an alias I created in my cygwin ~/.bash_login file:

alias cd-p="cd /cygdrive/c/projects/"
alias cd-mem="cd //g1/PatrickK/documents/my-memory/"

To list current aliases:

alias

echo

Print to the screen:

echo java $OPTIONS

set

Environment Variables

Date

Command Completion

An introduction to bash completion

Can be loaded by typing into your shell:

. /etc/bash_completion

You can put that line (with the . ) in ~/.bashrc.

Once this is done you’ll be able to TAB-complete many common arguments to programs, for example:

$ apt-get upd[TAB]
$ apt-get upg[TAB]

To revert to standard filename expansion try Shift, Tab or Esc, Tab.

Comments

Special Characters

Lines beginning with a # (with the exception of #!) are comments and will not be executed:

# This line is a comment.
echo "A comment will follow." # Comments may also occur following the end of a command.

Configuration

Reload

To reload a configuration file (I think this creates a new shell):

source ~/.bash_login

Note: . is a shortcut for source

Editing

Chaining Commands

Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed:

clear;date

Commands separated by && are executed in turn provided that the previous command has given a return value of true (zero):

command-1 && command-2 && command-3 && ... command-n

History

From Bash tips and tricks:

To make bash append history instead of overwriting it, and makes it so that each time the prompt is shown it writes out all the history:

shopt -s histappend
PROMPT_COMMAND=history -a

27/09/2011, Removed a couple of control characters from around history above. Not sure if this is correct, or not.

See Shortcut Keys.

To skip duplicate entries, add the following to ~/.bashrc:

export HISTCONTROL=ignoreboth

or:

export HISTCONTROL="ignoredups"

Long Command Lines

If you want to break up a command so that it fits on more than one line, use a backslash (\) as the last character on the line. Bash will print the continuation prompt, usually a >, to indicate that this is a continuation of the previous line.

Environment

Command line editing was not working when I was using a Solaris server. To enable command line editing type these commands when you log in:

bash
export PS1="\u@\h:\w> "

Control Flow

for

In this example, BING is a variable, and list.out contains a space separated list of things:

for BING in `cat list.out`; do echo $BING; done

Note:

for i in $( ls ); do
    echo item: $i
done

Execute

# Execute another script (do not fork or exec):
. /etc/profile.d/alljava.sh

If… else…

if [ -n "$JAVA_HOME" ]; then
    echo "JAVA_HOME is set to $JAVA_HOME"
else
    # set JAVA_HOME etc
    . /etc/profile.d/alljava.sh
    echo "Setting JAVA_HOME to $JAVA_HOME"
fi

Empty

Variables

Files

~/.bashrc

Default environment for all bash shells.

~/.bash_profile

Environment used for interactive shells.

Parameters

Command line parameters:

$1 $2 $3 $4

The number of command line parameters:

$#

To get the result of the last command (this can be used at the command line):

$?

Test

The test builtin command returns 0 (True) or 1 (False), depending on the evaluation of an expression:

> test 3 -gt 4 && echo True || echo False
False

-gt operator performs an arithmetic comparison. You can compare arithmetic values using one of -eq, -ne, -lt, -le, -gt, or -ge.

You can also use square brackets: test expr and [ expr ] are equivalent.

You can examine the return value by displaying $?:

> [ "abc" != "def" ];echo $?
0

You can compare strings using the operators =, !=, <, and >. The unary operator -z tests for a null string, while -n or no operator at all returns True if a string is not empty.

Note: the < and > operators are also used by the shell for redirection, so you must escape them using \\< or \\>:

~> test "abc" = "def" ;echo $?
1
~> [ "abc" != "def" ];echo $?
0
~> [ "abc" \< "def" ];echo $?
0
~> [ "abc" \> "def" ];echo $?
1
~> [ "abc" \< "abc" ];echo $?
1
~> [ "abc" \> "abc" ];echo $?
1

In this example, the value of the HOME variable is tested to see if it is a directory using the -d unary operator:

> test -d "$HOME" ;echo $?
0

Some of the more common file tests:

Operator

Characteristic

-d

Directory

-e

Exists (also -a)

-f

Regular file

-h

Symbolic link (also -L)

-p

Named pipe

-r

Readable by you

-s

Not empty

-S

Socket

-w

Writable by you

-N

Has been modified since last being read

In addition to the unary tests above, you can compare two files with the binary operators:

Operator

True if

-nt

Test if file1 is newer than file 2. The modification date is used for this and the next comparison.

-ot

Test if file1 is older than file 2.

-ef

Test if file1 is a hard link to file2.

Use help test for more options.

The -o operator allows you to test various shell options that may be set using set -o option.

The -a and -o options allow you to combine expressions with logical AND and OR, respectively, while the unary ! operator inverts the sense of the test:

~> test 1 = 1 -a 2 \< 3 ; echo $?
0
~> test 1 = 1 -o 2 \< 2 ; echo $?
0

You may use parentheses to group expressions and override the default precedence. Remember that the shell will normally run an expression between parentheses in a subshell, so you will need to escape the parentheses using \\( and \\) or enclosing these operators in single or double quotes.

vi Mode

Improve your interactive programming using the vi mode…

To enable vi mode in bash, add to your .bashrc in your home directory:

set -o vi