How to avoid tilde ~ in Bash prompt?

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

1

3 Answers

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

PS1='\h:$(pwd)\n$'
6

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.

4

I am using below setting in my .bash_profile file

$ export PS1='\e[1;34m\D{%T %d.%m.%Y} \[\033[00;39m\]\u\[\033[00;32m\]@$IP:\[\033[00;33m\]$(pwd)/\n\\$\[\033[00m\] '
21:40:08 14.12.2017 vmware@10.112.202.228:/home/vmware/Downloads/

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like