help-bash
[Top][All Lists]
Advanced

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

Re: Fixing escape format when values are empty


From: Mike Jonkmans
Subject: Re: Fixing escape format when values are empty
Date: Thu, 8 Apr 2021 09:29:17 +0200

On Thu, Apr 08, 2021 at 05:47:25AM +0200, pauline-galea@gmx.com wrote:
> I am using the following format to use colour when displaying text.
> an is the attribute number, bn is the background number, fn is the
> foreground number.
> 
>   fmt="\e[${an};${bn};${fn}m"
>   printf "${fmt}%s\e[0m" "Jane"
> 
> But "an", "bn", "fn" can be empty, and I then have to
> fix fmt.
> 
> Currently I am using the following replacements, but unsure
> if this solves all problems.  Or perhaps there  are simpler
> way to modify fmt.
> 
>   fmt="\e[${an};${bn};${fn}m"
>   fmt=${fmt/";;"/";"}   # replace [;  with [
>   fmt=${fmt/"[;"/"["}   # replace [;; with [

See the section on Parameter Expansion in 'man bash'.

Instead of "${an};", use "${an}${an:+;}"


Regards, Mike Jonkmans



reply via email to

[Prev in Thread] Current Thread [Next in Thread]