[SOLVED] Collapsing a Bash array into a comma-separated string
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Collapsing a Bash array into a comma-separated string
I'm looking to find Bash-only way to efficiently concatenate all positional parameters passed to a shell function. They should all end up in a single string but separated by commas. What I came up with first was this:
Code:
tb ()
{
local files=$(realpath $@);
local att=$(echo $files | tr ' ' ',');
echo foo "a='$att'"
}
However, that will choke on file names and paths containing spaces. I see several other ways, but they all seem to leave a trailing comma and that would break things. Or they require calling sed or other programs.
How should I really merge $1, $2, $3, ..., $n into a single, comma-separated string using just bash?
I will take your thread title: Collapsing a Bash array into a comma-separated string literally:
Code:
array=( ... ) # assuming this is filled already
string=""
for i in "${array[@]}" # this exact syntax will preserve array elements even if they have spaces in them
do
string="$i,$string"
done
# we have a superfluous trailing comma now, remove it:
string="${string%,}"
tb() { local c i str;
for i in "${@}"; do
i=$(realpath $i);
str="${str}${c}${i}"
c=','
done;
echo str=$str; }
It seems there are problems reading from realpath even with the --zero option. As an example, the following also chokes on file names and paths with spaces:
Code:
tx() { local c i str;
while read -r -d '' i; do
str="${str}${c}${i}"
c=','
done < <(realpath --zero ${@});
echo str=$str; }
It seems there are problems reading from realpath even with the --zero option. As an example, the following also chokes on file names and paths with spaces:
Try quoting the argument to realpath...
Code:
tb() { local c i str;
for i in "${@}"; do
i=$(realpath "$i");
str="${str}${c}${i}"
c=','
done;
echo str=$str; }
Or...
Code:
tx() { local c i str;
while read -r -d '' i; do
str="${str}${c}${i}"
c=','
done < <(realpath --zero "${@}");
echo str=$str; }
Last edited by astrogeek; 05-19-2019 at 03:23 AM.
Reason: Remove orphan QUOTE
I always found bash "interesting" with quotes - a PITA actually. Not worth the grief - if another tool does it easier/better I'll use that.
But I do admire the persistence of others, and the solutions you all come up with.
I always found bash "interesting" with quotes - a PITA actually. Not worth the grief - if another tool does it easier/better I'll use that.
But I do admire the persistence of others, and the solutions you all come up with.
I'm not keen on bash much either but it is the default shell for many distros and a few other operating systems.
The actual problem I am trying to solve is a work-around for Thunderbird's inability to accept relative paths for attachments submitted via the shell. So I have made a shell function to first calculate the full absolute path of each attachment and then submit all that to Thunderbird in such a way that they are attached to a new message.
I'm open for suggestions on other ways to solve that.
As for shells, I've tended to stay with defaults but have been experimented more and more with zsh on my GNU/Linux systems and am contemplating eventual migration.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.