Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


How do I set my Terminal / shell PS1 prompt?

I have Mac OS X 10.5.8. When I try to change my unix shell prompt by putting the line:

  export PS1='mac> '

into my .bash_profile, the prompt is changed but the cursor is in the first column (over the "m" of my prompt) and any typing overwrites the prompt.


Dave's Answer:

You've got the basics nailed, though there's a ton of cool things you can do with your shell prompt above and beyond what you're trying to accomplish. The problem you're having is that I'm going to guess that you're using a different editor, not one within the Terminal.app environment on your Mac OS X system, and it's saving the file with the wrong kind of end-of-line sequence.

Many editors have configuration options on file saving that let you choose Mac or MSDOS line ending sequences: I'll bet you have it set for the wrong one for this specific file. Try doing this:

cat ~/.bash_profile

next time you're running in Terminal and I'm pretty confident that it'll display incorrectly.

The real solution is to either use an editor within the Terminal environment (the two main choices are "vi" and "emacs", both of which are quite sophisticated editors) or to use a third-party tool like BBEdit or TextWrangler, tools that understand the Linux environment lurking underneath your Mac OS X world.

Once you've nailed this problem, here are a few fun things you can do with the command prompt in Bash:

SequenceExplanation
\hthe hostname up to the first `.'
\Hthe hostname
\tthe current time in 24-hour HH:MM:SS format
\Tthe current time in 12-hour HH:MM:SS format
\@the current time in 12-hour am/pm format
\uthe username of the current user
\wthe current working directory, with $HOME abbreviated with a tilde
\Wthe basename of the current working directory, with $HOME abbreviated with a tilde
\!the history number of this command
\$if the effective UID is 0, a #, otherwise a $

My command prompt, for example, is this:

PS1="\w (\!) \$ "

This gives me output like "~/bin (337) $ " when I'm in my "bin" directory, working. Experiment, play, you'll be surprised how helpful these can be once you really start digging into the command line!


More Useful Mac OS X Help Articles:
✔   Convert FLAC audio files to MP3 tracks on my Mac?
My son is in a band and he frequently sends me songs that they've recorded for me to hear what they're playing. Problem...
✔   Audacity can't find LAME library, I can't save Mp3?
Hey Dave. I read your article Audacity can't save mp3 audio files and am still puzzled because I downloaded the LAME Mp3 converter...
✔   How to remove Dashboard as a "space" in Mac OS X Spaces?
I'm a big fan of the Spaces utility in Mac OS X that lets me have multiple virtual screens [see Set Up Mac...
✔   Best place to buy a cheap MacBook laptop?
Hi Dave. I am looking for two gently used MacBook laptops for my teen daughters. Personal computers would greatly facilitate their studies as...
✔   File too big error copying to USB flash drive on my Mac?
I'm baffled. I have a 16GB Kingston USB flash drive that I use on my Mac system and I'm trying to copy a...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  





Categorized: Mac OS X Help   (Article 9116, Written by )
Tagged: bash, command line, linux, shell prompt, unix
Previous: Why don't my banner ad stats match Google Analytics stats?
Next: Why does email from my iPhone have the wrong name?




Reader Comments To Date: 5

Ken B said, on October 9, 2009 10:48 AM:

The problem with your "cat" command is that, generally, if the file has Windows' CRLF line endings, it will still look fine. Most Unix systems I use have a "-v" flag to cat which will show any control characters (aside from the newline) as caret-letter. So, if the example .bash_profile were in Windows format, using the command:

cat -v ~/.bash_profile

you would see:

export PS1='mac> '^M

John said, on October 10, 2009 4:51 PM:

Hi, I'm having the same problem with my command prompt, but my cat -v output appears to be ok with every line ending in ^M:

$cat -v ~/.bash_profile
export PS1="[\t] \w\n\u@\h\$ "^M
export CLICOLOR=1^M
export LSCOLORS=ExFxCxDxBxegedebegacab^M

What am I missing?

Dave Taylor said, on October 10, 2009 7:54 PM:

As Ken said, you don't want to have those ^M sequences. That's the problem. :-)

Dan said, on November 3, 2009 8:12 AM:

"tools that understand the Linux environment lurking underneath your Mac OS X world."

Dave, please print a correction here. There is no Linux environment under OS X. It's a Unix environment. Linux is a Unix-Like operating system. The above information has the potential to send unknowing users to Linux help sites and publications for their Mac, only to find out that the tools on a Linux distro may differ greatly from those on OS X. OS X is closer to BSD Unix, and even that's not a perfect match.

Again, please correct this for the sake of your readership.

Dave Taylor said, on November 3, 2009 8:24 AM:

Fair observation, Dan. I should probably say "Linux-like" since it's actually quite complicated to figure out the family tree and heritage of any given *nix.

The point of it was, though, that if you were to pick up a Linux book and/or visit some Linux help sites, you'd be able to glean quite a bit about how to work with Mac OS X on the command line. It's that Posix compliance bit and it's having the same shell and same basic commands.

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.