问题
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