-1

I am automating a script in real-time and based on some variables' values I want to append different string versions into the script I am building. to simplify the case, here is an example:

someenvvar=true

and I want to have a condition on this boolean variable within an echo, so I would expect something like:

echo "podman run {$someenvvar == true ? --net=myhost : --net=anotherhost}" >> test_script but the above command gives me the following output inside the script:

podman run { == true ? --net=myhost : --net=anotherhost}

I need to check several conditions within the same echo command and thus I seek the shortest version of inlined if conditions (if exists).

I know I can use if [<condition>]; then <true statements> elseif <false statements> fi inside the script but that is not what I want because I want to fill the script in realtime and need to have online echo command with possibility to check multiple environment variables within it. Your insights are much appreciated.

1
  • 1
    bash does not have boolean variables. A bash variable can hold a string, and also has arrays. You have to model your idea of a boolean somehow using strings. There is some builtin support which makes it easier to test, whether a string is empty or not, and also some support for dealing with strings which look like a whole number, and some people peruse this and represent, for instance, the concept of false by an empty string, or by the string 0, but of course you can also rule out your own mapping between the abstract idea of true/false and a concrete string. Commented Aug 30, 2022 at 9:27

2 Answers 2

1
$ someenvvar=0

$ echo podman run `[ $someenvvar = 1 ] && echo --net=myhost || echo --net=anotherhost`
podman run --net=anotherhost

$ someenvvar=1

$ echo podman run `[ $someenvvar = 1 ] && echo --net=myhost || echo --net=anotherhost`
podman run --net=myhost
1
  • 1
    Perhaps better not using the obsolete backquotes, but $(....) instead. Commented Aug 30, 2022 at 9:33
0

I am not completely sure what you want to achieve, but maybe that helps you: You can put your command inside round parenthesis like this:

example=0    
echo "podman run $(if [ $example = 1 ]; then echo "a"; else echo "b"; fi)"
4
  • thanks for the answer, it did eventually output podman run b but with "bash: [true]: command not found...", I believe that's the reason for why it headed to else statement
    – 5h3re3n
    Commented Aug 30, 2022 at 8:33
  • 1
    I added whitespaces around the condition, know it should work as expected
    – HFabi
    Commented Aug 30, 2022 at 11:07
  • For what it's worth, if [ true ] works, but doesn't do what you think it does. "true" is not an empty string, so if [ false ] would do the same thing. The idiomatic way to say that is if true anyway.
    – tripleee
    Commented Aug 31, 2022 at 4:47
  • true can be replaced with any condition (I edited the code to make it more clear), please correct me if I am wrong here?
    – HFabi
    Commented Aug 31, 2022 at 17:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.