This month's tip is a something useful for those of us who still prefer to use the command line.
Why have a two-line prompt? Well, many of us use the prompt to include additional information than merely a '$
' or '#
'.
In fact many prompts can (and do) hold a variety of information including:
I prefer a prompt that holds the following information:
While my prompt does not contain a lot of options, it can become quite long depending on the directory in which I am located. For example, the default prompt for the Bash shell on SuSE looks like the following:
fafrak@suselin:~ $
The prompt seems fairly reasonble until you find yourself several levels down into the bowels of a lengthy directory structure.
fafrak@suselin:~/work/BRUDEN-OSSG/Consulting/042/sys/0001 $
At this point, the prompt is a little unwieldy due to the length of the CWD. What are we to do?
In Bash, the prompt is altered by setting the PS1
shell variable.
$ export PS1="\n[\w]\n\u@\h $ "
[~/work/BRUDEN-OSSG/Consulting/042/sys/0001]
fafrak@suselin $
The content of the prompt is:
\n
Insert a newline.
[\w]\n
Insert a '[
' followed by theCWD
(\w
), then a ']
' followed by a newline.
\u@\h $
Insert the username (\u
), an '@
', the short hostname (\h
), a space, a '$
', and another space.
In the ksh like Bash, the prompt is altered by setting the PS1
shell variable. Unlike Bash, there are no special key codes to provide information to the prompt.
This approach works on Linux and Mac OS X - basically the 1993 version of Korn shell specification.
$ export PS1='
[${PWD#$HOME/}]
$(whoami)@$(hostname -s) $ '
[work/BRUDEN-OSSG/Consulting/042/sys/0001]
fafrak@suselin $
The content of the prompt is as follows:
'
The initial single quote is followed by a carriage return.
[${PWD#$HOME/}]
Insert a '
[
' followed by thePWD
variable (${PWD}
) that is stripped of user's home directory and leading (#$HOME/
), then a ']
' followed by a carriage return.
$(whoami)@$(hostname -s) $ '
Insert the username (
$(whoami)
), an '@
', the short hostname ($(hostname -s)
), a space, a '$
', and another space.
This approach works with AIX, HP-UX, Solaris, and Tru64 UNIX - basically the 1988 version of Korn shell specification.
USER
variable if it is not already set.
$ if [[ ${USER:-0} = 0 ]]; then export USER=$(whoami); fi
OS
variable.
$ export OS=$(uname -s)
HOSTNAME
variable.
$ if [[ $OS = "HP-UX" ]] || [[ $OS = "SunOS" ]]; then
export HOSTNAME=$(hostname | cut -d. -f1)
else
export HOSTNAME=$(hostname -s)
fi
$ export PS1="\\
"'[${PWD#$HOME/}]'"\\
$USER@$HOSTNAME $ "
[work/BRUDEN-OSSG/Consulting/042/sys/0001]
fafrak@suselin $
The content of the prompt is as follows:
"\\
The initial single quote is followed by a carriage return.
"'[${PWD#$HOME/}]'"\\
Insert a '[
' followed by thePWD
variable (${PWD}
) that is stripped of user's home directory and leading (#$HOME/
), then a ']
' followed by a carriage return.
$USER@$HOSTNAME $ "
Insert the username ($USER
), an '@
', the hostname ($HOSTNAME
), a space, a '$
', and another space.
In the csh, we must take a slightly different approach since there is no PS1
variable.
$ alias sp 'set prompt = "\\
`whoami`@`hostname -s` $ "'
The contents of the prompt are as follows:
"\\
Insert a newline.
[$cwd]\\
Insert a '[
' followed by the contents of the$cwd
variable followed by a newline.
`whoami`@`hostname -s` $ "'
Insert the username (
`whoami`
), an '@
', the short hostname (`hostname -s`
), a space, a '$
', and another space.
$ sp
[/home/fafrak/work/BRUDEN-OSSG/Consulting/042/sys/0001]
fafrak@suselin $
cd
, pushd
, and popd
commands are used.
$ alias cd 'cd \!*; sp'
$ alias pushd 'pushd \!*; sp'
$ alias popd 'popd \!*; sp'
We hope that you've found this tip useful. If you have any questions, comments, or suggestions for future tips, please contact us.