A way to prevent bash from parsing command line w/out using escape symbols

后端 未结 3 1942
离开以前
离开以前 2021-01-17 06:04

I\'m looking for a way (other than \".\", \'.\', \\.) to use bash (or any other linux shell) while preventing it from parsing parts of command line. The problem seems to be

3条回答
  •  温柔的废话
    2021-01-17 07:09

    Short answer: you can't do it, because the shell parses the command line (and interprets things like "&") before it even gets to the point of deciding your script/alias/whatever is what will be run, let alone the point where your script has any control at all. By the time your script has any influence in the process, it's far too late.

    Within a script, though, it's easy to avoid most problems: wrap all variable references in double-quotes. For example, rather than curl -o $outputfile $url you should use curl -o "$outputfile" "$url". This will prevent the shell from applying any parsing to the contents of the variable(s) before they're passed to the command (/other script/whatever).

    But when you run the script, you'll always have to quote or escape anything passed on the command line.

提交回复
热议问题