如何从 shell 获取准确的命令行字符串? [英] How to get exact command line string from shell?

查看:24
本文介绍了如何从 shell 获取准确的命令行字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 $*$@"$@" 甚至 ${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:

这篇关于如何从 shell 获取准确的命令行字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆