Preserving escapes in bash arguments $@

前端 未结 1 557
长发绾君心
长发绾君心 2021-01-06 23:31

related to this: Preserve Quotes in bash arguments

A simple example, where I simply run a command with nohup...

#!/bin/bash
nohup \"$@\"         


        
相关标签:
1条回答
  • 2021-01-07 00:38

    You need to use an array.

    cmd=( "$@" )
    printf '%q\n' "${cmd[@]}"
    nohup "${cmd[@]}"
    

    Scalar variables (strings) are NUL-delimited, so they can't contain an argument list (which is, by its nature, NUL-separated).

    See also the BashSheet entry on arrays, BashFAQ #5 (explaining how to use arrays), and BashFAQ #50 (explaining the pitfalls caused by not doing it this way).

    0 讨论(0)
提交回复
热议问题