60

I would like to display current path in sh prompt (not bash shell), which currently just shows "#", I tried with introducing this

env PS1="$(whoami)@$(hostname):$(pwd)"

and

set PS1="$(whoami)@$(hostname):$(pwd)"

in /etc/profile.

But as obvious this does not refresh when the the directory is changed or user changes. Please suggest a way to make this dynamic.

3
  • 2
    Note that each $() runs a separate program; it would be faster to use environment variables, such as $LOGNAME, $HOSTNAME and $PWD instead. May 29, 2013 at 12:00
  • One answer was to use single quotes instead of double quotes, however, that's quite the full correct answer. What you really want to do is defer evaluation of the code inside your prompt until the prompt is used.
    – MaasSql
    Oct 27, 2014 at 14:40
  • All you need is to use export "PS1="$(whoami)@$(hostname):$(pwd) >" then edit /etc/profile and append this line at the end.
    – SDsolar
    Mar 29, 2017 at 19:25

8 Answers 8

94

Command substitutions in double quotes " get expanded immediately. That is not what you want for your prompt. Single quotes ' will preserve the substitutions in $PS1 which then get only expanded when displaying the prompt. Hence this should work:

export PS1='$(whoami)@$(hostname):$(pwd)'

If you want the usual dollar sign and a space at the end of your prompt, simply add $ at the end (no escaping necessary): export PS1='$(whoami)@$(hostname):$(pwd)$ '

6
  • 1
    Worked like a charm after changing 'set' to 'export' in your answer export PS1='$(whoami)@$(hostname):$(pwd)$' I saved the change s to /etc/profile. Thank you.
    – Bleamer
    May 29, 2013 at 10:22
  • @Bleamer: set worked for me (but I didn't had a native sh). But I'll change it to export to comply with your setup.
    – mpy
    May 29, 2013 at 11:01
  • 2
    Is there any way to make this permanent? Currently I have to do it each time I log in. Thanks!
    – the.ufon
    Sep 2, 2014 at 7:07
  • 3
    You have put this line to /etc/profile (see question) or ~/.profile?!
    – mpy
    Sep 2, 2014 at 16:39
  • +1, this would need some delimiter at the end, though. As is, you get something like foo@localhost:/home/fools -la when using ls -la.
    – phresnel
    Jan 6, 2015 at 10:00
16
sh-4.2$ export PS1="\u@\h:\w>"
jenny@serenity:~>cd /usr/local
jenny@serenity:/usr/local>
6
  • 1
    I am afraid that works for bash shell not for sh, when i do this I get \u@\h:\w> as my command prompt
    – Bleamer
    May 29, 2013 at 10:11
  • 1
    Must be a different sh version; as you can see from the first line, it worked for me in sh 4.2.
    – Jenny D
    May 29, 2013 at 10:12
  • 1
    That may be the case. This shell is from Busy Box. Thank you. Appreciate your help though.
    – Bleamer
    May 29, 2013 at 10:25
  • @Bleamer, it works for me with BusyBox v1.19.4 built-in shell (ash).
    – cjm
    Sep 11, 2013 at 4:26
  • Thank you for response @cjm, though I'll avoid digging further into this.
    – Bleamer
    Sep 27, 2013 at 9:34
9

This command works for me.

export PS1="\u@\h: \W:$"

Where
\u = username
\h = hostname
\W Name of present folder (not full path)

2
  • +1 for \W parameter
    – Dimitry K
    Oct 23, 2018 at 17:31
  • My terminal keeps: \W # from PROMPT="%F{green}\W # %F{reset}". How fix?
    – Augusto
    Jul 4, 2022 at 23:56
6

One might consider to pimp the prompt by adding some colors. For instance:

export PS1='\[\e[0;36m\]\u\[\e[0m\]@\[\e[0;33m\]\h\[\e[0m\]:\[\e[0;35m\]\w\[\e[0m\]\$ '
2
  • 2
    This is what I was looking for. I hope everyone like some colours in life :) Sep 19, 2019 at 4:56
  • At least I do. ;)
    – Arvid
    Sep 19, 2019 at 17:38
2

One answer was to use single quotes instead of double quotes, however, that's not quite the full correct answer. What you really want to do is defer evaluation of the code inside your prompt until the prompt is used.

set PS1="$(pwd)" 

sets the prompt to the working directory as of the set command.

set PS1="\$(pwd)" 

does NOT expand $(pwd). Instead, PS1 is set to the literal value of $(pwd).

Test / Understand this by running:

echo $PS1

. If you see the string : $pwd, your prompt works. If you see the literal path, the prompt is broken because it has been statically set

1
  • This was exactly what I was looking for. Thanks! Simplest answer.
    – Forrest
    Feb 21 at 14:30
1

Use the below command to set is like in the cpanel.

export PS1='$(whoami)@${HOSTNAME%%.*} [$(pwd)]# '

1
  • Thanks! None of the other options above worked. This one did. Was driving me crazy. Thanks for saving my sanity. HA!
    – Lee_Str
    Jun 14, 2016 at 12:57
1

Try this colorful MULTILINE prompt Add this line

export PS1="[\e[1;33m\u\e[m@\e[1;36m\h\e[m] [\$(date +%k:%M:%S)]\n\e[0;32m[\w]\e[m \n\$ "

Prompt will be:

[yourusername@hostname] [17:34:13]
~   <----- this will be your working directory
>
0
  1. vim ~/.bashrc
  2. add following lines:
# notice the tailing space
export PS1='$(whoami)@$(hostname):$(pwd)# '
  1. open a new termal and you will find :
root@6e5efa720515:/opt/myapp#

You must log in to answer this question.

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