Hi i’m trying to make something like this to work in bash
JavaScript
x
$ http=xx sudo echo $http
xx
but i keep getting an empty line, the only thing that works so far is:
JavaScript
$ 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
JavaScript
$ 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.
JavaScript
$ http=xx sudo echo $http
Try instead
JavaScript
$ http=xx sudo -E bash -c 'echo $http'
The syntax is described in man env
JavaScript
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:
JavaScript
$ ( export http=xx ; sudo -E 'echo $http' )