94

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?

3
  • 1
    there must be a screen in preferences of Terminal App. which you can change behaviors.
    – fsonmezay
    Jan 31, 2011 at 16:00
  • 1
    Linux 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
    Jan 31, 2011 at 18:37
  • 1
    Have you guys tried this on newer macks? Its simply not working for me. It adds the raw regex string on the terminal \u@\H [\w]$, not the correspondent values Feb 2, 2023 at 15:44

7 Answers 7

125

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
8
  • 1
    Or just enter that PS1 assignment in every open window. By the way, the export is unnecessary. Jan 31, 2011 at 16:37
  • 7
    Actually, 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
    Jan 31, 2011 at 16:49
  • 1
    export 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
    May 25, 2015 at 7:53
  • 1
    would it be possible to explain what PS1, \u@\H:\w$ means?
    – Jas
    Jun 15, 2016 at 10:33
  • 4
    @Jas I've updated my answer accordingly, don't know why I didn't do this years ago ^^
    – Asmus
    Jun 15, 2016 at 10:42
12

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

[email protected] [/Applications/MAMP/htdocs]$ 
11

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$ "
0
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

1
  • Why don't I see any difference between '\w' and '\W' on MacBook? I wanter full path, but can't see it.
    – Danijel
    Sep 4, 2019 at 12:38
3

If you don't want to edit your ~/.bash_profile you can also just configure your terminal-profile. Click on Terminal > Prefences and from there go to the Profiles tab and then to the Shell tab. Enable [x] Run command and enter export PS1='\u@\H:\w$ '.

It's just another way of doing it.

enter image description here

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

1

Another solution would be to make a very simple change through a framework such as oh-my-zsh, as explained in this question:

If you use the oh-my-zsh framework for instance, depending on the theme you chose to use, you'd only have to replace the %c character with %~. The reason being that %c shows the current folder, while %~ shows the full path from the root.

The file where you'd want to make the change would be something like:
~/.oh-my-zsh/themes/<chosen_theme>
In my case, same as the linked question, I've selected the robbyrussell theme, so the file I had to modify was:
~/.oh-my-zsh/themes/robbyrussell.zsh-theme

For safety, you can also copy the original PROMPT= line at the end of~/.zshrc before customising your theme.

You must log in to answer this question.