test
string comparison
if [ X"$str1" = X"$str2 ]; then
  ...
fi

number comparison
if [ "$nb1" -eq "$nb2 ]; then
  ...
fi

check that we are in a subdirectory defined by FOOBAR (but FOOBAR may be undefined, in this case no error occurs)
if ( expr `pwd` : ${FOOBAR:-@}'.*' 1>/dev/null )
  then
     echo "we are in $FOOBAR"
  fi

loops
for file in *.{a,so}
  do
    echo $file
    nm $file | grep foobar
  done


loop on file content

while read l
do
  group=`echo "$l" | sed 's/.*<//'`
  if [ "$group" = "*" ]; then
    group="s"
  fi
  echo "$l" >> l_$group
done < verb.txt

predefined variables
$! : pid of the last process launched in background
 
retrieve the directory containg the script
if [ $0 = `basename $0` ]
 then
   myfullname=`which $0`
 else
   myfullname=$0
 fi
mydir=`dirname $myfullname`

~/.bashrc

if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi
PS1="\W \!>" noclobber=
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias h=history



Last update: September 1st, 2004 - Laurent