Thursday, January 13, 2011

Bash Prompt

Here is my favorite PS1 variable setting for the bash prompt:

export PS1="\! \u@\h \D{%Y-%m-%d} \t \w:-)"

Comment:
:-)             - a smiley for a happy day!
\w - full working directory, an absolute necessity.
\D{%Y-%m-%d} \t - short iso date and current time including seconds.
\u@\h - who are you, and where... could be quite expensive to get this wrong.
\! - look up the bash documentation for the !<hist number> history expansion feature to conveniently reuse previous commands or command parameters. With \! in the prompt you need the history command less to look up previous commands, but otherwise ommit this information in the prompt if your prompt line has already become too wide.
And here is an example how the output looks like:
237 clemens@gavrilov 2011-01-13 19:43:44 ~/dev/src:-)echo 'hi'
hi
238 clemens@gavrilov 2011-01-13 19:43:48 ~/dev/src:-)
For original documentation check "man bash" and jump to the PROMPTING section.

Saturday, January 1, 2011

Shell Script App Dir

#! /bin/sh

if [ "$APPHOME" = "" ] ; then
# try to locate application home directory

## resolve links - $0 may be a link to the app home
PRG=$0
progname=`basename $0`

while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG="`dirname $PRG`/$link"
fi
done

export APPHOME=`dirname "$PRG"`/..
export APPHOME=$(cd $APPHOME >/dev/null; pwd)
fi

Shell ISO Date and Time

ISO Date and Time in a command line:

date '+%Y-%m-%d %H:%M:%S'