The Computer Oracle

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

Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Life in a Drop

--

Chapters
00:00 Question
00:43 Accepted answer (Score 94)
01:23 Answer 2 (Score 16)
01:34 Answer 3 (Score 9)
01:56 Answer 4 (Score 6)
02:13 Thank you

--

Full question
https://superuser.com/questions/601181/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #shell #sh #busybox

#avk47



ACCEPTED ANSWER

Score 95


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:

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): PS1='$(whoami)@$(hostname):$(pwd)$ '




ANSWER 2

Score 16


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



ANSWER 3

Score 9


This command works for me.

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

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




ANSWER 4

Score 7


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



ANSWER 5

Score 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