Linux Box Admin
Trusted Remote Administration
logo

Tilde
What's new
Articles
Micro HowTos
About
Contact







Bash
(0 votes)
Wednesday, 07 March 2007
   
    Bash    
     
       
 

Bash initialization

When you first login, bash reads these initialization files in order (if they exist):

/etc/profile -- systemwide profile applies to all users

Then, it looks for these files and executes the FIRST one it finds:

~/.bash_profile
~/.bash_login
~/.profile

For interactive non-login shells, it executes:

~/.bashrc

At logout, it looks for this file to execute:

~/.bash_logout

The amazing technicolor multiline bash prompt

Set the PS1 environment variable in bash to customize the prompt. This is the prompt I use. It works best with a black background. If you want to keep it, add it to one of your bash startup files (like .bashrc).
PS1='\[\e[32;1m\]\u@\h \[\e[33;1m\][\w]\n\[\e[36;1m\]\t\[\e[0m\] $ '

Built-in shell variables:

$# number of command line arguments
$? exit value of last command
$$ process ID of current process
$! process ID of last background process $0 command name
$n where n=1-9 are the 1st thru 9th command line arguments
$* all command line arguments
$@ all command line arguments, individually quoted ($1 $2 ...)

If statement

if condition ; then
  commands
elif condition ; then
  commands
else
  commands
fi

Test the return status of the previous command:

if [ $? == 0 ] ; then
  commands
fi

Loops

while condition; do
  commands
done

for var in list; do
  commands
done

for (( expr1; expr2; expr3 )); do
  commands
done

Case statements

The case statement can be used in place of a complex if statement:
case expression in
pattern)
  commands
   ;;
pattern)
  commands
   ;;
*
  commands
esac

Traps

Bash scripts can trap signals to handle error processing better or unexpected events (like the user killing the script).

This traps signal(s) and executes "command" instead:
trap "command" signal [signal ...]

You can list active traps with:
trap -p

You can reset traps with:
trap - signal [signal ...]

   
       
         
 

Copyright © 2006,2007 Linux Box Admin.

 
My NHL fan blog