Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to remove the tilde from displaying within the PS1 variable.

My current PS1 string:

PS1="\h:\w\n$"

And the prompt looks like this:

lnx-hladky:/tmp/plugtmp
$

I don't like that the $HOME directory is displayed as tilde. Can this be avoided?

It causes problems, example:

lnx-hladky:~/DOC
$  

Documentation says:

\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

Is there any possibility to avoid $HOME being abbreviated with a tilde?

I have found one way around but I feel like it's overcomplicated:

PROMPT_COMMAND='echo -ne "\e[4;35m$(date +%T)\e[24m$(whoami)@$(hostname):$(pwd)\e[m\n"'
PS1=$

Can anyone propose a better solution? I have a feeling it's not quite OK to run so many commands just to get prompt. (date,whoami,hostname,pwd).

share|improve this question

2 Answers 2

up vote 5 down vote accepted

bash runs expansions in the prompt; just make sure to escape them.

PS1='\h:$(pwd)\n$'
share|improve this answer
    
That answers my question. I didn't know there is such possibility. Thanks a lot, Ignacio! –  Jirka Jan 13 '11 at 14:28
1  
@Jirka: You can also use: `PS1='\h:$PWD\n$'. –  Dennis Williamson Jan 13 '11 at 16:04
    
Jirka's solution executes pwd on every prompt. Dennis' solution does not, just fetches environment variable PWD, which is much cheaper. –  Stéphane Gourichon Nov 16 '13 at 20:57
    
pwd is a bash builtin. The assertion that ${PWD} is "much cheaper" than $(pwd) is without evidence. –  Vineet Aug 19 at 22:10

You don't need to run as many commands as you showed in your example. bash provides shortcuts for most of the things you mentioned.

Your example:

PROMPT_COMMAND='echo -ne "\e[4;35m$(date +%T)\e[24m$(whoami)@$(hostname):$(pwd)\e[m\n"'
PS1=$

can be rewritten as :

PS1='\e[4;35m\t\e[24m\u@\h:\w\e[m\n'

Where \t shows the time (in 24 hour format), \u shows the current username, '\h' shows the hostname -- the bash man page discusses these and the rest of the escapes available for your prompt.

Even if you expand the ~ to the full path, if you don't know which user is running the command and you're switching users regularly, you can create problems with file permissions or executable permissions.

share|improve this answer
    
Hi, my intention was to replace \w with the full path including FULL $HOME path. Thanks to Ignacio I'm now using PS1='\e[4;35m\t\e[24m\u@\h:$(pwd)\e[m\n$' which has solved my problem:-) –  Jirka Jan 13 '11 at 14:36
    
Perfect -- I like including \u so that I know who I am. Don't forget to accept Ignacio's answer -- click the check box to the left of the question. –  Doug Harris Jan 13 '11 at 15:05
    
Thanks for the hint. I'm new to this forum. It's a great place. –  Jirka Jan 13 '11 at 15:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.