比方說,我有以下這裏文檔(這是用來啓用Ubuntu的休眠):如何在傳遞給Bash的字符串中使用Bash變量?
IFS= read -d '' text << "EOF"
[Re-enable hibernate by default in upower]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=yes
[Re-enable hibernate by default in logind]
Identity=unix-user:*
Action=org.freedesktop.login1.hibernate
ResultActive=yes
EOF
我的方式在受保護的文件,該文本存儲類似如下:
echo "${text}" | sudo tee /etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla
我怎麼能做一個類似的操作使用bash變量在一個命令字符串(這是使用重定向運算符)傳遞給根下運行的Bash?
sudo bash -c 'echo "${text}" > /etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla'
在Bash的新實例中,變量顯然不存在。
不它的工作如果你說'sudo bash -c「echo \」$ text \「>/etc/....」'?它適用於這些雙引號。 – fedorqui
在'sudo'中不運行'echo'看起來對我來說是最好的解決方案。 – tripleee