Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: can't build array in right side of pipe

Tags:

arrays

bash

shell

Anyone have a clue as to why this code is not working as expected?

$> svnTags=()
$> svn ls http://plugins.svn.wordpress.org/duplicate-post/tags/ | while read line; do slashless=$(sed 's#/$##g' <<< $line); echo "slashless - $slashless"; svnTags+=($slashless); done
slashless - 1.0
slashless - 1.1
slashless - 1.1.1
slashless - 1.1.2
slashless - 2.0
slashless - 2.0.1
slashless - 2.0.2
slashless - 2.1
slashless - 2.1.1
slashless - 2.2
slashless - 2.3
$> echo "$svnTags[@]"

Not giving any output, I'm expecting it to output the built array of the svn tags.

Second command broken out:

svn ls http://plugins.svn.wordpress.org/duplicate-post/tags/ | while read line; do
    slashless=$(sed 's#/$##g' <<< $line)
    echo "slashless - $slashless"
    svnTags+=($slashless)
done
like image 264
jondavidjohn Avatar asked Jul 25 '12 18:07

jondavidjohn


1 Answers

Because what happens after | is a subshell. Variables changed in a subshell do not propagate back to the parent shell.

Common workaround:

while read line ; do
    ...
done < <(svn ls http://...)
like image 59
choroba Avatar answered Nov 06 '22 19:11

choroba