Bash quoted array expansion

前端 未结 6 947
借酒劲吻你
借酒劲吻你 2021-01-31 02:57

WHen I write a bash program I typically construct calls like follows:

declare -a mycmd=( command.ext \"arg1 with space\" arg2 thing etc )

\"${mycmd[@]}\" || ech         


        
6条回答
  •  借酒劲吻你
    2021-01-31 03:13

    I like to put the code in a function so it is easier to reuse and document:

    function myjoin
    {
       local list=("${@}")
       echo $(printf "'%s', " "${list[@]}")
    }
    
    declare -a colorlist=()
    colorlist+=('blue')
    colorlist+=('red')
    colorlist+=('orange')
    
    echo "[$(myjoin ${colorlist[@]})]"
    

    Note how I added the comma in the solution because I am generating code with a bash script.

    [EDIT: fix problem that EM0 pointed out that it is returning ['blue',]]

提交回复
热议问题