简体   繁体   中英

From Bash script, how do I call a Linux environment variable contained in a command string?

I have an environment variable:

export qua="key={1234}"

How do I call $qua from within a Bash script that is running a Chocolatey command?

#!/bin/bash
salt computername chocolatey.install qualys install_args='"\"$qua\""'

I cannot get Chocolatey to read the environment variable properly. I have also tried:

install_args='"$qua"'

and

install_args=$qua

The command works if I run:

#!/bin/bash
salt computername chocolatey.install qualys install_args="key={1234}"

As somebody pointed out, sudo creates a new environment that does not retain your exported variables. Observe:

$ export this=that
$ env | grep this
this=that
$ sudo env | grep this
$

To pass the variable through, you need something like:

$ sudo env this=$this | grep this
SUDO_COMMAND=/usr/bin/env this=that
this=that

So in your case, your script could be something like:

#!/bin/bash
salt computername chocolatey.install qualys install_args="$ARG"

without extra quotes. And you would call it like:

sudo env ARG="$qua" ./script.sh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM