Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I join elements of an array in Bash?

Tags:

arrays

bash

If I have an array like this in Bash:

FOO=( a b c ) 

How do I join the elements with commas? For example, producing a,b,c.

like image 440
David Wolever Avatar asked Oct 06 '09 17:10

David Wolever


People also ask

How do you join array elements?

Array.prototype.join() The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

How do you concatenate in bash?

Bash is a widely used shell in Linux, and it supports the '+=' operator to concatenate two variables. As the example above shows, in Bash, we can easily use the += operator to concatenate string variables. Bash's += works pretty similar to compound operators in other programming languages, such as Java.

How do you join an element to an array of strings?

The arr. join() method is used to join the elements of an array into a string. The elements of the string will be separated by a specified separator and its default value is a comma(, ).

How do I echo an array in bash?

How to Echo a Bash Array? To echo an array, use the format echo ${Array[0]}. Array is your array name, and 0 is the index or the key if you are echoing an associative array. You can also use @ or * symbols instead of an index to print the entire array.


2 Answers

A 100% pure Bash function that supports multi-character delimiters is:

function join_by {   local d=${1-} f=${2-}   if shift 2; then     printf %s "$f" "${@/#/$d}"   fi } 

For example,

join_by , a b c #a,b,c join_by ' , ' a b c #a , b , c join_by ')|(' a b c #a)|(b)|(c join_by ' %s ' a b c #a %s b %s c join_by $'\n' a b c #a<newline>b<newline>c join_by - a b c #a-b-c join_by '\' a b c #a\b\c join_by '-n' '-e' '-E' '-n' #-e-n-E-n-n join_by , # join_by , a #a 

The code above is based on the ideas by @gniourf_gniourf, @AdamKatz, @MattCowell, and @x-yuri. It works with options errexit (set -e) and nounset (set -u).

Alternatively, a simpler function that supports only a single character delimiter, would be:

function join_by { local IFS="$1"; shift; echo "$*"; } 

For example,

join_by , a "b c" d #a,b c,d join_by / var local tmp #var/local/tmp join_by , "${FOO[@]}" #a,b,c 

This solution is based on Pascal Pilz's original suggestion.

A detailed explanation of the solutions previously proposed here can be found in "How to join() array elements in a bash script", an article by meleu at dev.to.

like image 199
Nicholas Sushkin Avatar answered Oct 10 '22 01:10

Nicholas Sushkin


Yet another solution:

#!/bin/bash foo=('foo bar' 'foo baz' 'bar baz') bar=$(printf ",%s" "${foo[@]}") bar=${bar:1}  echo $bar 

Edit: same but for multi-character variable length separator:

#!/bin/bash separator=")|(" # e.g. constructing regex, pray it does not contain %s foo=('foo bar' 'foo baz' 'bar baz') regex="$( printf "${separator}%s" "${foo[@]}" )" regex="${regex:${#separator}}" # remove leading separator echo "${regex}" # Prints: foo bar)|(foo baz)|(bar baz 
like image 38
doesn't matters Avatar answered Oct 10 '22 00:10

doesn't matters