bug-bash
[Top][All Lists]
Advanced

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

Re: ${parameter/pattern/string} behaviour changed


From: Greg Wooledge
Subject: Re: ${parameter/pattern/string} behaviour changed
Date: Fri, 16 Mar 2018 16:43:07 -0400
User-agent: NeoMutt/20170113 (1.7.2)

On Fri, Mar 16, 2018 at 08:18:53PM +0100, dc@ftb-esv.de wrote:
>       Command: x="a b c"; echo "'${x// /','}'"
>       On old systems the output is:  'a','b','c'
>       In this version the output is: 'a,b,c'

So, this LOOKS like an attempt to take a list, and write it out with
single quotes around each element, and commas BETWEEN the quoted
elements (but no trailing or leading comma).  Obviously this is not
the best way to store a list, nor is it the best way to write a list
to stdout with those modifications.

If you want to continue along these cheap hack lines, then you need
to store the quote character in a variable, and use that inside the
parameter expansion.  Thus:

wooledg:~$ string="a b c"; q=\'; echo "'${string// /$q,$q}'"
'a','b','c'

But this is really not the way I would approach the problem.  Granted,
bash is not a great language for operating on lists, but still....

Printing a list, with modifications, WITH an extra trailing comma,
is trivial:

wooledg:~$ list=(a b c); printf "'%s'," "${list[@]}"; echo
'a','b','c',

Given this, probably the *easiest* way to get the desired result without
the extra trailing comma would be to remove it afterward:

wooledg:~$ list=(a b c); printf -v tmp "'%s'," "${list[@]}"; echo "${tmp%,}"
'a','b','c'

I would probably go with that, if I had to do this in bash.



reply via email to

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