tags:

views:

63

answers:

2
+2  Q: 

Echo expanded PS1

I have a shell script that runs the same command in several directories (fgit). For each directory, I would like it to show the current prompt + the command which will be run there. How do I get the string that corresponds to the decoded (expanded)PS1? For example, my default PS1 is ${debian_chroot:+($debian_chroot)}\[\e[1;32m\]\u\[\e[0m\]@\[\e[1;32m\]\h\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]$(__git_ps1 ' (%s)')$, and I'd like to echo the resulting prompt username@hostname:/path$, preferably (but not necessarily) with the nice colors. A cursory look at the Bash manual didn't reveal any definite answer, and echo -e $PS1 only evaluates the colors.

+1  A: 

Why don't you just process the $PS1 escape substitutions yourself? A series of substitutions such as these:

p="${PS1//\\u/$USER}"; p="${p//\\h/$HOSTNAME"

By the way, zsh has the ability to interpret prompt escapes.

print -P '%n@%m %d'

or

p=${(%%)PS1}
Dennis Williamson
A: 

You may have to write a small C program that uses the same code bash does (is it a library call?) to display that prompt, and just call the C program. Granted, that's not very portable since you'll have to compile it on each platform, but it's a possible solution.

tomlogic