0

I want to compare if a String variable is empty and giving the result to the boolean variable without doing a if statement

I tried with the test command:

wordEmpty=$( [ "$word" = "" ] )

but that doesn't work. I get the error that the "permission denied" at that line. I'm using the standard POSIX Shell, not Bash.

Is it even possible without a if statement?

  • I don't understand. If `$word` is not empty, then you want `word` to contain... what? What if $word is empty? Should the new value of `word` to contain some kind of boolean value? – glenn jackman Jun 18 '22 at 16:11
  • Sorry, I changed it. – AnonymousBoy Jun 18 '22 at 16:53
  • `that the "permission denied" at that line` That is a very odd error for that line. Please post the exact full verbatim error message. – KamilCuk Jun 18 '22 at 17:31
  • 1
    `$( )` only captures stdout; it does not store exit status. In this case, a command substitution to capture stdout is overhead you don't need -- there's no reason to pay the performance penalty when you can just use `myCommand arg1 arg2...; myCommand_rc=$?` – Charles Duffy Jun 18 '22 at 19:46
  • BTW, bash doesn't particularly have a concept of a "boolean variable". Almost all variables are strings or arrays; you have to go out of your way, using a command like `declare` or `typeset`, to get anything else, and you can treat strings _as if they were_ other non-array types without issue -- treat your string as an integer and it'll act like one. And POSIX sh has even less of an idea of non-string types than bash does. – Charles Duffy Jun 18 '22 at 20:22
  • The fact that there's no boolean type is why KamilCuk is recommending that for POSIX sh you store the string "true" or the string "false", or for bash store "1" or "0". Personally, I prefer to store a number even in POSIX; `rc=$?` lets you run `if [ "$rc" = 0 ]` to check if the value is true, and doesn't lend itself to security bugs the way `was_success=true; if $was_success; then ...` does. – Charles Duffy Jun 18 '22 at 20:23

1 Answers1

0

There are no booleans and there are no boolean expressions in Bash. Every value is a string. The only available data type is a string.

It's typical to represent a boolean value in shell with a string true and a string false or a string 0 and a non-zero string as true.

How to assign a variable a boolean expression? Is it even possible without a if statement?

The command [ sets the exit status to 0 if the expression is logically true, and sets to non-zero otherwise. Just set the variable to the value of the exit status of [ command.

wordEmpty=$( [ "$word" = "" ]; echo $? )
if [ "$wordEmpty" -eq 0 ]; then
   something
fi

However, because true and false are commands that exit with exit status 0 and non-0 respectively, I prefer to use the strings in POSIX shell, because they look nicer.

wordEmpty=$( if [ "$word" = "" ]; then echo true; else echo false; fi )
if "$wordEmpty"; then
   something
fi

In Bash shell I prefer numerical values, because I can just do if ((wordEmpty)).

tripleee
  • 175,061
  • 34
  • 275
  • 318
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Tangentially perhaps see also [Why is testing ”$?” to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – tripleee Jun 18 '22 at 18:20
  • 2
    The command substitution is overkill. `[ "$word" = "" ]; wordEmpty=$?` would be sufficient. – chepner Jun 18 '22 at 19:34
  • I think also a possible way should be -> `wordEmpty=$( [ "$word" = "" ] && echo true || echo false )` – AnonymousBoy Jun 18 '22 at 23:10