[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Colorized Prompts Problem



Petter Adsen wrote:
> Brad Rogers wrote:
> > PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\ [\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
> 
> Excuse me if this is a dumb question, but what does the "debian_chroot"
> part (twice) do?

Basic shell substitution.  The task there is to include "(foo)" in the
prompt if debian_chroot is set to foo.  Initially you might think, I
will use $debian_chroot and if it is empty then it will be empty and
if it is set to foo then it will expand to foo.  Almost works.  That
would expand to "foo" not "(foo)" with the parens around it.

We could almost do this with an "if then" test.  In concept we want:

  if [ -n "$debian_chroot" ]; then
    emit "($debian_chroot)"
  fi

That is basically what we want to do.  But the above isn't the right
way to do it in the shell.  Instead we want to use shell parameter
expansion.  There are many commands available.  One is the colon and
plus operators.

  man bash

   ${parameter:+word}
      Use  Alternate Value.  If parameter is null or unset, nothing is
      substituted, otherwise the expansion of word is substituted.

If $parameter is null or unset then do nothing.  The entire ${...}
disappears.  But if $parameter contains a value then replace the
${...} part with the value of word.  It is recursive so word may
contain another variable expansion.

  ${debian_chroot:+($debian_chroot)}

If $debian_chroot is set then replace the grouping with "($debian_chroot)"
with the parens around it.

The ${parameter:+word} construct is used a lot of shell programming.

Editorial remark: I really wish the author of /etc/debian_chroot had
simply made that /etc/chroot_name or some such.  Because by naming it
/etc/debian_chroot guarantees that no other distribution will pick
that convention up and use it.  If it had been /etc/chroot_name or
something non-distribution specific then it would have been possible
to share across other distributions.  Sigh.

Bob

Attachment: signature.asc
Description: Digital signature


Reply to: