简体   繁体   中英

bash assign a value to variable if environment variable is set in a single line

How do set a variable value to something based on an environment variable is set.

For example, I want it to be

var1 = var2 if ENV_VARIABLE==true else var3

(using python syntax for simplicity)

I want this to happen in a single line as well. What is the correct syntax in this case in bash. The guide at https://unix.stackexchange.com/questions/122845/using-ab-for-variable-assignment-in-scripts is good but does not cover this case.

尝试这个:

[[ "$ENV_VARIABLE" == "true" ]] && var1="$var2" || var1="$var3"        

这是移植到UR- sh ,技术上可能把在一行。

case $ENV_VARIABLE in true) var1=var2;; *) var1=var3;; esac
var1 = var2 if ENV_VARIABLE==true else var3

So:

var2=smth
var3="other smth"
ENV_VARIABLE=true

we can (the best, does not remove trailing newlines):

if [ "$ENV_VARIABLE" = true ]; then var1=$var2; else var1=$var3; fi

or (assuming ENV_VARIABLE is a string "true" or a string "false" only):

if "$ENV_VARIABLE"; then var1=$var2; else var1=$var3; fi

or (removes trailing newlines)

var1=$(if "$ENV_VARIABLE"; then echo "$var2"; else echo "$var3"; fi)

or (removes trailing newlines)

var1=$("$ENV_VARIABLE" && echo "$var2" || echo "$var3")

or (removes trailing newlines and eval is evil)

var1=$(eval echo "\$$("$ENV_VARIABLE" && echo var2 || echo var3)")

or (eval is evil)

eval var1=\$$(arr[0]=var2; arr[1]=var3; [ "$ENV_VARIABLE" = true ]; echo "${arr[$?]}")

or (eval is evil)

eval var1=\$$("$ENV_VARIABLE" && echo var2 || echo var3)

or (removes trailing newlines)

printf -v var1 "%s" "$("$ENV_VARIABLE" && echo "$var2" || echo "$var3")"

or (crazy)

var1=$("$ENV_VARIABLE"; printf "%s\x00" "$var2" "$var3" | head -z -n$(($?+1)) | tail -z -n1 | tr -d '\000')

or (named reference variable, so not really assignment, more a pointer)

declare -n var1=$("$ENV_VARIABLE" && echo "var2" || echo "var3")

Puff, out of ideas. I honesty think that only the first one is the proper one. I sometimes in my scripts use booleans variables as true and false strings, which allows for removing only the [ ... = true ] parts.

if statement can be written on a single line.

-z can be used to test if a variable is defined and has a value.

-a in the test, stands for AND

== EDIT ==

(see remark from @tripleee: the first version contains a -a which is not reliable. I did not found an issue with the first version proposed, but it seems to be a good practice to avoid the use -a and -o )

Give a try to this:

if [[ ! -z "${ENV_VARIABLE}" && "${ENV_VARIABLE}" = true ]] ; then var1="${var2}"; else var1="${var3}"; fi

.

.

== FIRST VERSION ==

if [ ! -z "${ENV_VARIABLE}" -a "${ENV_VARIABLE}" = true ] ; then var1="${var2}"; else var1="${var3}"; fi

Use default and alternate values together.

var1=${ENV_VARIABLE:+$var2} var1=${var1:-$var3}

$ var2=foo
$ var3=bar
$ var1=${ENV_VARIABLE:+$var2} var1=${var1:-$var3}
$ echo $var1
bar
$ 
$ var1=${HOME:+$var2} var1=${var1:-$var3}
$ echo $var1
foo

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