Brace expansion from a variable in Bash

后端 未结 2 860
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 18:41

I would like to expand a variable in Bash. Here\'s my example:

variable=\"{1,2,3}\"
echo $variable

Expected output:

1 2 3
         


        
2条回答
  •  臣服心动
    2021-01-21 19:05

    This won't work first because double-quotes suppress brace expansion:

    variable="{1,2,3}"
    

    This still won't work because bash will not assign a list to a variable:

    variable={1,2,3}
    

    The solution is to assign your list to an array:

    $ variable=( {1,2,3} )
    $ echo "${variable[@]}"
    1 2 3
    

提交回复
热议问题