Passing empty arguments to sudo -i

只谈情不闲聊 提交于 2020-12-12 10:36:32

问题


Here is an example bash script that I would like to run using sudo -i:

#!/bin/bash

echo "arg 1: $1"
echo "arg 2: $2"

When I run this command normally with one empty argument, it runs as expected:

$ /tmp/args.sh "" two
arg 1:
arg 2: two

With plain sudo, I get the expected result:

$ sudo /tmp/args.sh "" two
arg 1:
arg 2: two

However if I use -i (to pick up the user's shell and login scripts), suddenly the first argument disappears:

$ sudo -i /tmp/args.sh "" two
arg 1: two
arg 2:

And I cannot figure out any way to quote or escape the first, empty argument.

Note: I have figured out a workaround by writing /tmp/args.sh "" two to a file and then executing that with sudo -i, but I was wondering if there is any way to achieve this directly from the command line.


回答1:


This works for me:

sudo -i bash -c '/tmp/args.sh "" two'



回答2:


Quote your empty double-quoted arguments with single quotes, like this:

$ sudo -i /tmp/args.sh '""' two


来源:https://stackoverflow.com/questions/27892812/passing-empty-arguments-to-sudo-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!