How do I break up an extremely long string literal in bash?

后端 未结 6 1110
独厮守ぢ
独厮守ぢ 2021-01-30 10:21

I would like to embed a long command like this in a bash script:

mycommand \\
    --server myserver \\
    --filename extremely/long/file/name/that/i/would/like/         


        
6条回答
  •  伪装坚强ぢ
    2021-01-30 11:17

    It's a bit of a hack, but this works:

    mycommand \
        --server myserver \
        --filename "extremely/long/file/name/"`
                   `"that/i/would/like/to/be/able/to/break/"`
                   `"up/if/possible" \
        --otherflag \
        --anotherflag
    

    Bash concatenates string literals that are adjacent, so we take advantage of that. For example, echo "hi" "there" prints hi there whereas echo "hi""there" prints hithere.

    It also takes advantage of the backtick operator, and the fact that a bunch of spaces evaluates to nothing.

提交回复
热议问题