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 change my PS1 command prompt in Bash?

I have read about 15 chapters of your book (Teach Yourself UNIX in 24 Hours) so far. We are using vi. On the PS1 command, how do I see what is in it so I can add to it and not change what is already there? We are using bash. What I see now is: ctdaywbsdv:/u/gwilki$

When I enter ‘echo $PS1’ I get: $ \[\e[0m\]

How do I keep the above and add the histcmd option? What does the \e and 0m\ mean? What do the [] the square brackets mean? I can’t find a man page for PS1. How do I find the rest of the options for this PS1?


Dave's Answer:

You're right that there's no man page for the PS1 prompt setting in Bash, but the bash man page itself (type man bash) is incredibly long, and one section explains all the features and capabilities of the PS1 prompt variable. In that it notes, for example, that:

The value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string. The default value is \s-\v\$

Going down to the section labeled PROMPTING, it explains all the different variables you can utilize to set or customize your own Bash prompt. It's incredibly long:

CodeExplanation
\aan ASCII bell character (07)
\dthe date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format}the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
\ean ASCII escape character (033)
\hthe hostname up to the first `.'
\Hthe hostname
\jthe number of jobs currently managed by the shell
\lthe basename of the shell's terminal device name
\nnewline
\rcarriage return
\sthe name of the shell, the basename of $0 (the portion following the final slash)
\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
\Athe current time in 24-hour HH:MM format
\uthe username of the current user
\vthe version of bash (e.g., 2.00)
\Vthe release of bash, version + patchelvel (e.g., 2.00.0)
\wthe current working directory
\Wthe basename of the current working directory
\!the history number of this command
\#the command number of this command
\$if the effective UID is 0, a #, otherwise a $
\nnnthe character corresponding to the octal number nnn
\\a backslash
\[begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\]end a sequence of non-printing characters

To see the current value of your prompt, just type echo $PS1 in the Bash shell. To change it, simply change the value of that variable. To make the change automatic each time you log in, add that to your ~/.profile and it's done.

Now you have enough information to learn what the \e and \0m mean too. The former is the ASCII escape character and is likely being used to start a sequence to put your prompt in bold or inverse video, and \0m is indeed the sequence required to do just that, it's the \000 format being used.

I hope that helps you fine-tune your Bash prompt as needed!


More Useful Unix and Linux Help Articles:
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
✔   Can I script renaming files based on an XML data map?
I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing...
✔   Test for valid numbers in a Bash shell script?
In a different discussion on this site [see Redirecting input in a shell script] a visitor commented that "I was too busy trying...
✔   Review: iSSH for the iPad/iPhone
If you're running an online business like I am, there are times when you need to connect and log in to the server...

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: Unix and Linux Help   (Article 7458, Written by )
Tagged: bash, command shells, linux, unix
Previous: How do I change my return address in Google Gmail?
Next: Should I buy links from sites with higher PageRank?




Reader Comments To Date: 1

hvjadsf dfg said, on July 23, 2007 7:28 PM:

can you get a cds music on the psp

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!
Powered By
Linux Journal: Free Issue!


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.