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

07
2014-07
  • Bleamer

    I would like to display current path in sh prompt (not bash shell), which currently just shows "#", I tried with introducing this

    env PS1="$(whoami)@$(hostname):$(pwd)"
    

    and

    set PS1="$(whoami)@$(hostname):$(pwd)"
    

    in /etc/profile.

    But as obvious this does not refresh when the the directory is changed or user changes. Please suggest a way to make this dynamic.

  • Answers
  • mpy

    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:

    export PS1='$(whoami)@$(hostname):$(pwd)'
    
  • Jenny D
    sh-4.2$ export PS1="\u@\h:\w>"
    jenny@serenity:~>cd /usr/local
    jenny@serenity:/usr/local>
    

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