Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash add array string suffix to an array

I have two array bash, I want to ar array string to the end of name array.

array=(a b c)
name=(toto_ tata_)

result :

toto_a
toto_b
toto_c
tata_a
tata_b
tata_c

I try those command :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for i in "${name[@]}"
do
    arra=( "${i/%/$array[@]}" )
done
printf '%s\n' "${arra[@]}"

>Solution :

I’d recommend using a second for:

array=(a b c)
name=(toto_ tata_)
arra=()

for i in "${name[@]}"; do
    for j in "${array[@]}"; do
        arra+=("$i$j")
    done
done
printf '%s\n' "${arra[@]}"

Will produce:

toto_a
toto_b
toto_c
tata_a
tata_b
tata_c

Try it online!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading