如何从 shell 获取准确的命令行字符串?字符串、命令行、准确、shell

2023-09-06 18:23:56 作者:我早已无所谓

我知道 $*$@"$@" 甚至 ${1+"@"} 以及它们的含义.

I know about $*, $@, "$@" and even ${1+"@"} and what they mean.

我需要从 shell 脚本访问 EXACT 命令行参数字符串.请注意示例中的引号.像$@"这样的东西会保存参数但会删除引号,我不知道如何从中恢复.

I need to get access to the EXACT command-line arguments string from a shell script. Please pay attention to the quotes in the example. Anything like "$@" saves parameters but removes quotes and I don't see how to recover from this.

例子:

./my-shell.sh "1 2" 3

./my-shell.sh "1 2" 3

而且我需要在不进行任何处理的情况下检索 EXACT 参数字符串:

And I need to retrieve the EXACT parameter string without any processing:

1 2"3

知道如何实现吗?

推荐答案

bash 中,你可以从 shell 历史中得到这个:

In bash, you can get this from the shell history:

set -o history
shopt -s expand_aliases

function myhack {
  line=$(history 1)
  line=${line#*[0-9]  }
  echo "You wrote: $line"
}
alias myhack='myhack #'

如你所描述的那样工作:

Which works as you describe:

$ myhack --args="stuff" * {1..10}    $PATH
You wrote: myhack --args="stuff" * {1..10}    $PATH

另外,这里有一个方便的图表:

Also, here's a handy diagram:

 
精彩推荐
图片推荐