This is not the proper syntax for setting an array. All this does is set the simple variable "arr" to the string value "name".
See here for the correct forms for setting and using arrays:
http://mywiki.wooledge.org/BashGuide/Arrays
http://mywiki.wooledge.org/BashFAQ/005
In addition, a function acts as a command, just like any other, which means it only executes if it's the first word in a command string. To use the output of one command inside another, you generally have to use a command or process substitution, or similar nested command form.
http://mywiki.wooledge.org/BashGuide...ands#Functions
http://mywiki.wooledge.org/CommandSubstitution
http://mywiki.wooledge.org/ProcessSubstitution
BTW, function definitions properly use either "
function foo", or "
foo()", but not "
function foo()".
http://mywiki.wooledge.org/BashPitfa...tion_foo.28.29
From the output of your command, you have to do some post-processing in order to extract only the second field. In this case awk is probably the best choice to use. So overall, you'd probably want to do something like this:
Code:
name(){
virsh list | awk '( $1 ~ /^[0-9]+$/ ) { print $2 }'
}
arr=( $( name ) )
echo "${arr[*]}"
...Or perhaps put the whole thing in the function, with something like this:
Code:
name(){
mapfile -t arr < <( virsh list | awk '( $1 ~ /^[0-9]$/ ) { print $2 }' )
}
name
echo "${arr[*]}"
Or even just use the command stand-alone, of course.