I have set my prompt in bash in a such way that I can use it directly in scp command:

My current PS1 string:

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

And the prompt looks like this:

lnx-hladky:/tmp/plugtmp
$

What I don't like at all is the fact that $HOME directory is displayed as tilde. Can this be avoided? It's causing problems when switching between different users.

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).

Thanks a lot!

Jirka

link|improve this question
feedback

2 Answers

up vote 3 down vote accepted

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

PS1='\h:$(pwd)\n$'
link|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
@Jirka: You can also use: `PS1='\h:$PWD\n$'. – Dennis Williamson Jan 13 '11 at 16:04
feedback

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.

link|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
feedback

Your Answer

 
or
required, but never shown

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