How to print current bash prompt?

The question is simple. I want to evaluate current value of PS1 in my bash script.

All materials on google point to tutorials on pimping it up, but I want to evaluate to see how would it be rendered by my current terminal, or at least by some terminal.

Is there any soft/function that would help me achieve that? Of course I'd like to have all escaped characters evaluated, so echo $PS1 is not that useful in my case.


One more possibility, using script utility (part of bsdutils package on ubuntu):

$ TEST_PS1="e[31;1mu@h:ne[0;1m$ e[0m"
$ RANDOM_STRING=some_random_string_here_that_is_not_part_of_PS1
$ script /dev/null <<-EOF | awk 'NR==2' RS=$RANDOM_STRING
PS1="$TEST_PS1"; HISTFILE=/dev/null
echo -n $RANDOM_STRING
echo -n $RANDOM_STRING
exit
EOF
<prints the formatted prompt properly here>

script command generates a file specified & the output is also shown on stdout. If filename is omitted, it generates a file called typescript.

Since we are not interested in the log file in this case, filename is specified as /dev/null . Instead the stdout of the script command is passed to awk for further processing.

  • The entire code can also be encapsulated into a function.
  • Also, the output prompt can also be assigned to a variable.
  • This approach also supports parsing of PROMPT_COMMAND ...
  • EDIT:
    It appears that the new version of script echoes the piped stdin in the typescript. To handle that, the above mechanism can be changed to:

    $ TEST_PS1="e[31;1mu@h:ne[0;1m$ e[0m"
    $ RANDOM_STRING=some_random_string_here_that_is_not_part_of_PS1
    $ script /dev/null <<-EOF | awk '{old=current; current=$0;} END{print old}' RS=$RANDOM_STRING
    PS1="$TEST_PS1"; HISTFILE=/dev/null
    alias $RANDOM_STRING=true
    $RANDOM_STRING
    $RANDOM_STRING
    EOF
    
    <prints the formatted prompt properly here>
    

    Explanation:

    Try entering these commands manually on the terminal. Copy these commands under the heredoc as they are and paste with mouse middle click. The script command's stdout would contain something very similar.

    eg With above case, the output of the script command gives this:

    PS1="e[31;1mu@h:ne[0;1m$ e[0m"; HISTFILE=/dev/null
    alias some_random_string_here_that_is_not_part_of_PS1=true
    some_random_string_here_that_is_not_part_of_PS1
    some_random_string_here_that_is_not_part_of_PS1
     e[0m"; HISTFILE=/dev/nullhsane-dev : ~/Desktop $ PS1="e[31;1mu@h:ne[0;1m$ 
    anishsane@anishsane-dev:
    $ alias some_random_string_here_that_is_not_part_of_PS1=true
    anishsane@anishsane-dev:
    $ some_random_string_here_that_is_not_part_of_PS1
    anishsane@anishsane-dev:
    $ some_random_string_here_that_is_not_part_of_PS1
    anishsane@anishsane-dev:
    $ exit
    

    Split that stdout with "some_random_string_here_that_is_not_part_of_PS1" as delimiter (record separator of awk) and print the last but one record.

    EDIT2:

    Another mechanism (using bash source code and gdb):

    $ gdb -batch -p $$ -ex 'call bind_variable("expanded_PS1", decode_prompt_string (get_string_value ("PS1")), 0)'
    $ echo "$expanded_PS1"
    <prints the formatted prompt properly here>
    
  • There is a tiny issue here though. The [ or ] strings in PS1 will get printed as 1 / 2 respectively. You can remove those with tr -d '12' <<< "$expanded_PS1"
  • If you get error like gdb failed to attach to the process (seems to happen in ubuntu :- ), run gdb with sudo .

  • Another way to do this would be to eval echoing your prompt to handle any expansion (was not sure why the brackets remain). This is most likely less robust than @anishsane's method, but may be a little quicker:

    show-prompt() {
        eval 'echo -en "'$PS1'"' | sed -e 's#[##g' -e 's#]##g'
    }
    # To show it in a function registered with `complete -F` on
    # a single tab, and keep the user's input:
    show-prompt
    echo -n "${COMP_WORDS[@]}"
    

    Had tinkered with that regarding this GitHub issue.


    尝试下面的命令

    echo $PS1 | 
    sed -e s/'d'/"$(date +'%a %b %_d')"/g | 
    sed -e s/'t'/"$(date +'%T')"/g | 
    sed -e s/'@'/"$(date +'%r')"/g | 
    sed -e s/'T'/"$(date +'%r'| awk {'print $1'})"/g | 
    sed -e s/'e'//g | sed -e s/'h'/"$HOSTNAME"/g | 
    sed -e s/'h'/"$HOSTNAME"/g | 
    sed -e s/'H'/"$HOSTNAME"/g | 
    sed -e s/'u'/"$USER"/g | 
    sed -e s@'W'@"$(pwd)"@g | 
    sed -e s/'w'/"$(pwd | sed -e s@$HOME@'~'@g )"/g | 
    sed -e s/"\"//g | 
    sed -e s/"["//g | 
    sed -e s/"]"/*/g | 
    cut -d'*' -f2 | 
    cut -d';' -f2 | 
    sed s/ //g | 
    sed -e s/[a-z]$/"$([ "$USER" != "root" ] && echo $ || echo #)"/g
    
    链接地址: http://www.djcxy.com/p/78032.html

    上一篇: 与ng的索引问题

    下一篇: 如何打印当前的bash提示符?