How to have full directory path always shown in mac terminal (like linux terminal)

74

36

In my ubuntu terminal I have always current directory shown completely. Just like this:

blabla@blublu:~/music/my_album/classical/beethoven$

But in my Mac (Mac OS X 10.6.5) terminal does not show the full path and it is like this:

blabas-MacBook-Pro:classical beethoven$

Is there anyway that I change mac terminal behavior to act like linux terminal?

Pouya

Posted 2011-01-31T15:46:03.943

Reputation:

1there must be a screen in preferences of Terminal App. which you can change behaviors. – None – 2011-01-31T16:00:20.970

1Linux terminals don't magically show the full path; it depends on your settings. Some linux distros configure things to show the full path by default, some don't. It all depends on the $PS1 variable. (See Asmus's answer.) – frabjous – 2011-01-31T18:37:20.353

Answers

112

To let bash return "user@hostname:path/to/directory$" as your prompt, add the following line to your ~/.bash_profile:

export PS1='\u@\H:\w$'

or

export PS1='\u@\H:\w$ '

if you like having a space between the $ and the command

to make the changes take effect immediately, run the following command in every open window (or restart Terminal):

source ~/.bash_profile

EDIT: A list of available strings can be found in the paragraph "PROMPTING" in the man page for bash (man bash):

PROMPTING

  When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command.  Bash allows these prompt strings  to  be  customized  by
   inserting a number of backslash-escaped special characters that are decoded as follows:
          \a     an ASCII bell character (07)
          \d     the 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
          \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, which could be used to embed a terminal control sequence into the prompt
          \]     end a sequence of non-printing characters

Asmus

Posted 2011-01-31T15:46:03.943

Reputation: 2 007

1export com is working as suggested and showing full path, but once I restart the terminal it shows only present directory (not full path). How can I keep this forever. – JiteshW – 2015-05-25T07:53:44.753

1would it be possible to explain what PS1, \u@\H:\w$ means? – Jas – 2016-06-15T10:33:28.373

4@Jas I've updated my answer accordingly, don't know why I didn't do this years ago ^^ – Asmus – 2016-06-15T10:42:13.413

Why don't I see any difference between '\w' and '\W' on MacBook? I wanter full path, but can't see it. – Danijel – 2019-09-04T12:36:51.117

@Danijel Hmm, not sure … for me, using \w returns ~/Documents and \W returns just Documents (if I'm within my /Users/myusername/Documents folder, of course). Perhaps you didn't start a new terminal session for a change to become active? Also, if I understood you correctly, you want to use $PWD instead, in order to get the full path. – Asmus – 2019-09-04T12:43:15.273

1Or just enter that PS1 assignment in every open window. By the way, the export is unnecessary. – Paused until further notice. – 2011-01-31T16:37:58.750

6Actually, if you don´t use export, your would be defining a shell variable, not an environment variable, so PS1 would not be passed globally to all processes. – Asmus – 2011-01-31T16:49:45.343

10

I am not sure about Mac, but in Ubuntu I've changed the Gnome Terminal prompt with

PS1="\a\n\n\e[31;1m\u@\h on \d at \@\n\e[33;1m\w\e[0m\n$ "

BZ1

Posted 2011-01-31T15:46:03.943

Reputation: 371

9

I made it to look very similar to centOS terminal on my Mac. Open bash_profile, on terminal

nano ~/.bash_profile

Add the following

# Show always fullpath on terminal
export PS1='\u@\H [\w]$ '

Restart Terminal and then it will looks like this

username@host.local [/Applications/MAMP/htdocs]$ 

Andres Ramos

Posted 2011-01-31T15:46:03.943

Reputation: 113

6

Inside PS1 value, "\w" represents the full path, where "\W" represents the current directory name only.

http://tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html

superNobody

Posted 2011-01-31T15:46:03.943

Reputation: 96

Why don't I see any difference between '\w' and '\W' on MacBook? I wanter full path, but can't see it. – Danijel – 2019-09-04T12:38:23.597

1

I just change the /w to /W to make just the current folder

in colored terminal

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '

no color terminal

    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\W\$ '

I use ubuntu 16.04 and change the file in ~/.bashrc

Afrijal Dzuhri

Posted 2011-01-31T15:46:03.943

Reputation: 11

0

I used this command. It works for me.

first,

vi ~/.bash_profile

then,add this words in a new line.

`export PS1='\u@\H:\w$ '`

finally,

`source ~/.bash_profile`

Fank Zhou

Posted 2011-01-31T15:46:03.943

Reputation: 1