-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 
Sign up to request clarification or add additional context in comments.

1 Comment

Perhaps better not using the obsolete backquotes, but $(....) instead.
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 Comments

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
I added whitespaces around the condition, know it should work as expected
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.
true can be replaced with any condition (I edited the code to make it more clear), please correct me if I am wrong here?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.