我有一个shell脚本test.sh
,如下所示。
sudo -H sh -c '
echo $1;
'
但是当我以./test.sh abcd
运行此脚本时,它并没有回应任何东西。然后我改变了我的脚本,如下所示。
sudo -H sh -c '
echo \$1;
'
但现在,它显示的输出为$1
。我需要做什么修改才能将输出设为abcd
。请提供建议,因为我是shell脚本的初学者。
感谢。
答案 0 :(得分:2)
试试这个:
sudo -H sh -c "
echo $1;
"
sudo
在一个新的shell中运行该命令,该shell不知道传递给它的父参数的任何信息,所以你需要在新的(子)shell中运行命令字符串之前展开它们
答案 1 :(得分:1)
尝试:
sudo -H sh -c '
echo "$1";
' argv0 "$1"
来自bash
手册页:
man bash | less -Ip '-c string'
# -c string If the -c option is present, then commands are read from
# string. If there are arguments after the string, they are
# assigned to the positional parameters, starting with $0.