[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Is there a way to contract y=${x[*]}; "${y// /}" into on
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] Is there a way to contract y=${x[*]}; "${y// /}" into one line |
Date: |
Fri, 27 May 2016 17:20:48 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Fri, May 27, 2016 at 03:58:07PM -0500, Peng Yu wrote:
> Hi, I use the following code to remove spaces from the string of
> "${x[*]}". But it requires two lines. Is there a way to do it in one
> line? Thanks.
>
> x=(a b c)
> y=${x[*]}
> echo "${y// /}"
Here it is in one line:
x=(a b c); y=${x[*]}; echo "${y// /}"
You're welcome.
Here's what you actually WANTED instead of what you WROTE about:
arraycat() {
local IFS=
declare -n __arraycat_x="$1"
printf '%s\n' "${__arraycat_x[*]}"
}
x=(a b c)
arraycat x
If you ask how to write the function in one line, I will give an answer
similar to my first answer in this email.
If you ask how to get around the need to write an ugly name for the
local name reference inside the function, I will refer you to the
prrevious 6 weeks of nameref discussion on bug-bash. It's not pretty.
(The *one* thing you want namerefs for -- passing variables to a function
by reference -- is a thing they can't do safely. Meh.)