Hi i’m trying to make something like this to work in bash
$ http=xx sudo echo $http xx
but i keep getting an empty line, the only thing that works so far is:
$ export http=xx $ sudo -E echo $http xx
what i would like to achieve is the ability to inline a variable for the sudo command
i also tried this as suggested here
$ sudo http=xx echo $http
but with no luck, am i missing something?
Advertisement
Answer
The problem with the first command is that http
variable expansion happens before it is set.
$ http=xx sudo echo $http
Try instead
$ http=xx sudo -E bash -c 'echo $http'
The syntax is described in man env
Some have suggested that env is redundant since the same effect is achieved by: name=value ... utility [ argument ... ]
Otherwise if the goal is not to affect current shell environment the export and sudo command can be done in a subshell:
$ ( export http=xx ; sudo -E 'echo $http' )