bash command substitution, functions and quoting : need help



Hello everybody.

I've got a question that's been driving me mad for a couple of days
now. It has to do with quoting, command substitution and functions.
I'm trying to generate a format string for PS1 within a function,
return that string from function (via echo) and assign THAT to PS1,
except for some reason the escaping goes all awry in the process.

Please note the prompts (everything below ">") in the below commands
execution sequence with lines numbered on the left (not part of prompt
per se) :

01 prompt> PS1='$( { echo "\[\033[1;34m\]$(date) >\[\033[0m\]
" ; } )'
02 Sat Apr 5 15:01:29 2008 >
03 Sat Apr 5 15:01:30 2008 >
04 Sat Apr 5 15:01:31 2008 > func() { echo "\[\033[1;34m\]$(date) >\
[\033[0m\] " ; } ; PS1='$(func)'
05 \[\033[1;34m\]Sat Apr 5 15:01:37 2008 >\[\033[0m\]
06 \[\033[1;34m\]Sat Apr 5 15:01:39 2008 >\[\033[0m\]
07 \[\033[1;34m\]Sat Apr 5 15:01:40 2008 >\[\033[0m\]

At #01 I assign the output of command substitution directly to PS1,
and as can be observed in #02-#04 it works out perfect (the coloring
worked, date evaluated every time, etc). At #04 I try to assign the
same "echo" output to PS1 but this time "echo" was executed in a
function. As can be observed in #05-#07 the escape sequences were
somehow "garbled" and instead of doing coloring they are reproduced
verbatim. I'm observing this with bash 2.05b and 3.x.

Any ideas as to how I can format the string including bash prompt
color escape sequences inside a function, using bash built-ins only
(e.g. no printf and such), then somehow have the function evaluated
every time and have the output stored into PS1, with all the escape
sequences intact?

Thanks in advance.
.



Relevant Pages

  • Re: bash command substitution, functions and quoting : need help
    ... At #01 I assign the output of command substitution directly to PS1, ... I'm observing this with bash 2.05b and 3.x. ... Any ideas as to how I can format the string including bash prompt ... color escape sequences inside a function, ...
    (comp.unix.shell)
  • Re: bash command substitution, functions and quoting : need help
    ... you can use the PROMPT_COMMAND variable (man bash for the ... and then pick up the results in PS1. ... ANSI escape sequences, when it's written to the screen the escape sequences ... defined set of color escapes that worked across ALL terminals, ...
    (comp.unix.shell)
  • BASH: awk command substitution
    ... I wonder why the following command substitution in bash runs all right when it's not quoted: ... but when I double-quote it I get an error: ... but I expected the shell to run the awk script the same way both in the unquoted command substitution and in the double-quoted one. ...
    (comp.unix.shell)
  • Re: () $() etc., in Bash
    ... So you should use $) for arithmetic substitution as it is ... standard though supported by bash, ksh and zsh) and be careful ... when using command substitution. ...
    (comp.unix.shell)
  • Re: How to change window manager for GNOME
    ... > It is a bash specific thing. ... The backticks are still the method for csh ... The $command substitution syntax was introduced in the ... The backtick form of command substitution can also be nested, ...
    (comp.os.linux.misc)