Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing empty arguments to sudo -i

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.

like image 613
morloch Avatar asked Oct 18 '25 15:10

morloch


1 Answers

This works for me:

sudo -i bash -c '/tmp/args.sh "" two'
like image 151
Barmar Avatar answered Oct 21 '25 03:10

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!