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

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.


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


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