How to display current path in command prompt in linux's sh (not bash)?

43

15

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.

Bleamer

Posted 2013-05-29T09:56:16.470

Reputation: 551

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 – 2014-10-27T14:40:44.640

All you need is to use export "PS1="$(whoami)@$(hostname):$(pwd) >" then edit /etc/profile and append this line at the end. – SDsolar – 2017-03-29T19:25:25.417

1Note that each $() runs a separate program; it would be faster to use environment variables, such as $LOGNAME, $HOSTNAME and $PWD instead. – user1686 – 2013-05-29T12:00:58.560

Answers

77

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)$ '

mpy

Posted 2013-05-29T09:56:16.470

Reputation: 20 866

2Is there any way to make this permanent? Currently I have to do it each time I log in. Thanks! – the.ufon – 2014-09-02T07:07:11.273

3You have put this line to /etc/profile (see question) or ~/.profile?! – mpy – 2014-09-02T16:39:53.603

+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 – 2015-01-06T10:00:06.063

Yes, add this to /etc/profile for system-wide, or ~/.profile for a single user.
All you need is to use export "PS1="$(whoami)@$(hostname):$(pwd) $" then edit /etc/profile and append this line at the end.
– SDsolar – 2017-03-29T19:26:29.367

1Worked 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 – 2013-05-29T10:22:34.337

@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 – 2013-05-29T11:01:22.383

14

sh-4.2$ export PS1="\u@\h:\w>"
jenny@serenity:~>cd /usr/local
jenny@serenity:/usr/local>

Jenny D

Posted 2013-05-29T09:56:16.470

Reputation: 520

1I am afraid that works for bash shell not for sh, when i do this I get \u@\h:\w> as my command prompt – Bleamer – 2013-05-29T10:11:44.170

1Must be a different sh version; as you can see from the first line, it worked for me in sh 4.2. – Jenny D – 2013-05-29T10:12:22.447

1That may be the case. This shell is from Busy Box. Thank you. Appreciate your help though. – Bleamer – 2013-05-29T10:25:56.680

@Bleamer, it works for me with BusyBox v1.19.4 built-in shell (ash). – cjm – 2013-09-11T04:26:08.680

Thank you for response @cjm, though I'll avoid digging further into this. – Bleamer – 2013-09-27T09:34:48.477

5

This command works for me.

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

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

rangsiman

Posted 2013-05-29T09:56:16.470

Reputation: 51

+1 for \W parameter – Dimitry K – 2018-10-23T17:31:20.717

3

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\]\$ '

Arvid

Posted 2013-05-29T09:56:16.470

Reputation: 131

1This is what I was looking for. I hope everyone like some colours in life :) – Govind Kailas – 2019-09-19T04:56:39.343

At least I do. ;) – Arvid – 2019-09-19T17:38:42.897

1

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

MaasSql

Posted 2013-05-29T09:56:16.470

Reputation: 111

1

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

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

Milan

Posted 2013-05-29T09:56:16.470

Reputation: 31

Thanks! None of the other options above worked. This one did. Was driving me crazy. Thanks for saving my sanity. HA! – Lee_Str – 2016-06-14T12:57:39.203