fritz stelluto
development for digital media
berlin, london

Setting A Bash Prompt in OS XHowTo

To change the command line prompt on bash, my shell of choice, you need to edit the file .bash_profile in your home directory. To do that, UNIX machos will tell you to use vi. This I find only slightly less painful than walking on my knees on dried chickpeas, and I won't touch it (I won't even touch vim, its marginally less awkward cousin).

Editing the .bash_profile file

Luckily for the rest of us, Mac OS X and many Linux distributions come with either nano or pico (or both). They are easy to use, lightweight command line editors, with a list of command shortcuts at the bottom. All you need to know to use them is that the symbol ^ stands for the [CTRL] key.
So, open up your Terminal (in /Application/Utilties on OS X), type
nano ~/.bash_profileand you are good to go (this will also create the file if it doesn't exist). After you edit and save the file, the changes will apply to all new windows, so you'll have to open a new one to see the new prompt in action.

The shell prompt is just an environmental variable like any other, loaded by the shell application as it starts up. It is called PS1 on bash (short for Prompt Shell, I suppose). There is also a PS2, which bash displays when more input is required; most people leave that as it comes by default, with a simple >.
You can put anything you want in your shell prompt(s), silly one liners like "Hello pretty!" and so on, as well as special characters that means something to bash, like "show the date" or "make a beep sound".

You can see what's currently stored in the environmental variable PS1 by typing
echo $PS1in your Terminal. Typically it will be something similar to
[\u@\h \W]\$(that's the default RedHat prompt). Those \u and \h are bash special characters (they stand for "username" and "the hostname up to the first '.'"). A complete list for your version of bash by typing man bash in your Terminal, which will give you the manual entry for bash. You'll have to use the keys [CTRL]-v many times to scroll down to a section called 'Prompting' (if you scroll past it, use [CTRL]-b to scroll back). The current list of special characters on my mac is at the bottom of this page.

My own prompt

I like my shell prompt to tell me the username (because sometimes you need to run commands as a different user, and I don't want to have to run whoami all the time), the time (useful when logging into remote machines, to find out their local time), the hostname (to remind me whether I am currently logged into my machine or a remote one), the current working directory (without having my home directory abbrievated to ~) and the history number of the command (so that I can run an earlier command by typing ! plus the number).

Putting all this together results in a long prompt, which I split into two lines - something that UNIX machos find shocking, but I don't like clutter.
export PS1=" \H>>\u>>\$PWD [\!][\d \A]$"The >, [, ], and $ are printed as they are, the rest are all special characters from the table at the bottom of the page, except for the sequence \$PWD, which causes bash to look up the value of another environmental variable, $PWD, and print its result. This is similar to the special character \W, except that the home directory is not abbrievated to ~, since I don't want to have to remember all the time whether I am in /Users/fritz on my Mac or /home/fritz on a remote machine.

Bash special characters (OS X version)

\a an ASCII bell character (07) \d the date in "Weekday Month Date" format (e.g., "Tue May 26") \D{format} a date formatted as per the strftime(3) function. The braces are required \e an ASCII escape character (033) \h the hostname up to the first '.' \H the hostname \j the number of jobs currently managed by the shell \l the basename of the shell's terminal device name \n newline \r carriage return \s the name of the shell, the basename of $0 (the portion following the final slash) \t the current time in 24-hour HH:MM:SS format \T the current time in 12-hour HH:MM:SS format \@ the current time in 12-hour am/pm format \A the current time in 24-hour HH:MM format \u the username of the current user \v the version of bash (e.g., 2.00) \V the release of bash, version + patch level (e.g., 2.00.0) \w the current working directory, with $HOME abbreviated with a tilde \W the basename of the current working directory, with $HOME abbreviated with a tilde \! the history number of this command \# the command number of this command \$ if the effective UID is 0, a #, otherwise a $ \nnn the character corresponding to the octal number nnn \\ a backslash \[ begin a sequence of non-printing characters \] end a sequence of non-printing characters

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strong>