command line - How to avoid tilde ~ in Bash prompt?

05
2013-12
  • Jirka

    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

  • Answers
  • Ignacio Vazquez-Abrams

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

    PS1='\h:$(pwd)\n$'
    
  • Doug Harris

    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.


  • Related Question

    linux - Show only current directory name (not full path) on bash prompt
  • obvio171

    The way my bash prompt is currently configured, it shows the whole path to the current directory. This is annoying when I'm deep inside a directory tree, as the prompt becomes so long that every command wraps into the next line. How do I make it show only the last part of the path?

    This is what I have in my .bashrc:

    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    
    # If this is an xterm set the title to user@host:dir
    case "$TERM" in
    xterm*|rxvt*)
        PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'
        ;;
    *)
        ;;
    esac
    

  • Related Answers
  • quack quixote

    Change the \w (lowercase) to \W (uppercase):

    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
                                                                                           ^^
               this one waaaaaay over here ------------------------------------------------+
    

    Have a look at the Bash Prompt HOWTO for lots of fun details. example:

    user@host:/usr/local/bin$ echo $PS1
    ${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ 
    
    user@host:/usr/local/bin$ export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\W\[\033[00m\]\$ '
    
    user@host:bin$
    

    The PROMPT_COMMAND variable, if set, is a command that gets run before displaying the prompt specified in PS1. In your case, PROMPT_COMMAND runs an echo statement with certain ANSI escape sequences that manipulate the titlebar of an Xterm.

    If you suspect your PROMPT_COMMAND is overriding your PS1 prompt, you can unset it and test things out:

    $ unset PROMPT_COMMAND
    

    Finally, be sure that you're changing the PS1 definition that actually gets used. Common locations are /etc/bash.bashrc, /etc/profile, ~/.bashrc, ~/.bash_profile, ~/.profile. The system files are generally (but not always) run before the user files.