14

In terminal I can always see which path I'm in:

macbook-romeo:School romeovs$ echo "example command"

which I find quite useful. Yet there is a lot of unnecessary stuff here too: macbook-romeo is not useful since I'm always on my macbook (duh). Then again romeovs is quite useful (this is my user name).

Is there a wan to edit what is shown here? e.g.

22:50 - School - Romeo$ echo "example"

where 22:50 is the current time? (This is just an example of course, I won't be needing the time, I'd use like the text to be a bit more minimalistic).

3 Answers 3

12

This OSX Daily article has some detailed instructions on how to do. In short you need to modify your ~/.bash_profile file and modify the PS1 variable.

Below is a list of possible variables you can have displayed:

  • \d – Current date
  • \t – Current time
  • \h – Host name
  • # – Command number
  • \u – User name
  • \W – Current working directory (ie: Desktop/)
  • \w – Current working directory, full path (ie: /Users/Admin/Desktop)
11

You need to modify the .bashrc file found in your home directory.

Open terminal and do the following:

  • cd ~
  • touch .bashrc
  • open .bashrc

Add the following line at the end of your .bashrc file

export PS1='\t - \W - \u\$ '

  • Save the file and close your editor
  • Go back to the terminal and enter the following command:

    source .bashrc

Boom! You should now have your desired prompt! (If desired prompt was your given example)

2
  • When I did this I got the following MBP-15:~ Daniel$ source .bashrc -bash: export: =': not a valid identifier -bash: export: \t - \W - \u$ ': not a valid identifier MBP-15:~ Daniel$ Commented Feb 12, 2012 at 22:10
  • @DanielCompton Thanks for pointing this out, I've updated my answer so that this works. :)
    – macaco
    Commented Feb 14, 2012 at 11:38
4

This is controlled by the shell variable $PS1

You can set this variable to whatever you want, either temporarily or more permanently with a .login or .profile script depending on what shell you use.

Take a look at the following output for examples:

Last login: Fri Oct 21 21:59:28 on ttys000

Dans-MacBook-Air:~ stuffe$ echo $PS1

\h:\W \u\$

Dans-MacBook-Air:~ stuffe$ PS1="Hello World $"

Hello World $PS1=`date`

Fri 21 Oct 2011 22:03:23 BSTsleep 60

Fri 21 Oct 2011 22:03:24 BSTPS1="`date +%H:%M` $"

22:04 $PS1="\h:\W \u\$"

Dans-MacBook-Air:~ stuffe$

The possibilities are endless. There's something you can do with PS2 also, but I think that only takes effect if you are root - details are hazy, my Unix is a long time ago and based on Solaris, not Mac OS X, but the fundamentals are the same.

Additional info: As suggested in other replies, there are built-in flags like \h and \W etc you can use, but you can also use the output of almost any command too, by using the "back ticks" (funny single quotes used in my date example above). Commands inside back ticks get run and the output substituted, allowing you to include anything, even if there is no built in flag, even for seemingly stupid stuff, whatever you can think of. Want to include the time of the last entry onto an error log file? Use something like:

PS1="`tail -1 $LOGFILE | awk '{print $1}'` $ "

(read the last line of the file referenced by $LOGFILE, and print the 1st column assuming a default field delimiter) etc etc. If you can execute it, you can include it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .