Bash shell脚本echo在sudo内部不工作

Bash shell脚本echo在sudo内部不工作,bash,shell,scripting,sh,Bash,Shell,Scripting,Sh,我有一个shell脚本test.sh,如下所示 sudo -H sh -c ' echo $1; ' 但是,当我以/test.sh abcd的身份运行此脚本时,它没有响应任何内容。然后我改变了我的脚本,如下所示 sudo -H sh -c ' echo \$1; ' 但现在,它将输出显示为$1。要获得输出为abcd,我需要在此处进行哪些修改。请给我一些建议,因为我是shell脚本的初学者 谢谢。试试这个: sudo -H sh -c " echo $1;

我有一个shell脚本
test.sh
,如下所示

sudo -H sh -c '
    echo $1;    
'
但是,当我以
/test.sh abcd
的身份运行此脚本时,它没有响应任何内容。然后我改变了我的脚本,如下所示

sudo -H sh -c '
    echo \$1;    
'
但现在,它将输出显示为
$1
。要获得输出为
abcd
,我需要在此处进行哪些修改。请给我一些建议,因为我是shell脚本的初学者

谢谢。

试试这个:

sudo -H sh -c "
  echo $1;    
"
sudo
在不知道传递给其父代的参数的新shell中运行命令,因此在新(子)shell中运行命令字符串之前需要展开这些参数。

尝试以下操作:

sudo -H sh -c "
  echo $1;    
"
sudo
在一个新的shell中运行命令,该shell不知道传递给其父级的参数的任何信息,因此您需要在新的(子)shell中运行命令字符串之前展开这些参数。

请尝试:

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.
尝试:

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.

完美的。谢谢,伙计:)完美的一个。谢谢你,伙计:)