I want to print the firewall-cmd --state status in variable - $STAT

and then compare the variable $STAT with "not running" string

but Comparison get false , it is because spaces inside the variable

please advice how to do it bash regular expiration

so I will check that "not running" contain in the variable so will print the "not running" by echo

#  firewall-cmd --state
   not running
   STAT=` firewall-cmd --state `
   echo $STAT
   not running
   [[ $STAT = "not running" ]] && echo "not running"

   no output 
  • 1
    Instead of echo $STAT, do printf "%q\n" "$STAT" so you can see exactly what's in it. This is necessary because = is exact string equality operator. – glenn jackman Nov 2 '17 at 18:30

This works just fine:

$ stat=$(echo "not running")
$ [[ $stat = "not running" ]] && echo yes

However, if you get leading or trailing spaces in the variable, it doesn't work. But you could use a pattern match or a regex match to cover for that:

$ stat=$(echo " not running ")
$ [[ $stat = *"not running"* ]] && echo yes
$ [[ $stat =~ "not running" ]] && echo yes

Both search for the string not running within the variable. The asterisks in the first one explicitly account for leading or trailing characters. The second is regex match which by default looks for a match anywhere within the string.

To match against leading or trailing spaces explicitly with a regex, it's easiest to put the regex in a variable. $pattern must be unquoted in the conditional.

$ pattern="^ *not running *$"
$ [[ $stat =~ $pattern ]] && echo yes

(I assumed only possible extra space characters here, not arbitrary whitespace.)

| improve this answer | |
  • 1
    For regex, use [[:space:]]* instead. That will match any whitespace, not just spaces. – glenn jackman Nov 2 '17 at 18:32
  • @glennjackman, yeah, if there's a chance of tabs or such. Trailing newlines shouldn't be a problem since the command substitution will eat them. – ilkkachu Nov 2 '17 at 18:37

You need to use quotes.

If the output of firewall-cmd --state is not running, then the command STAT=$( firewall-cmd --state ) will set the variable STAT to the string not, and then try to execute running.

What you want to do is:

STAT="$( firewall-cmd --state )"

You can do all of this at once without using a variable:

if [[ "not running" = "$( firewall-cmd --state )" ]]; then
    echo "It's not running"
    echo "It's running"
| improve this answer | |
  • 1
    +1 just for the good programming practice of putting the constant on the left of the equals sign. – Jeff Schaller Nov 2 '17 at 18:04
  • shell assignment doesn't need quotes, and neither does [[ .. ]]. try something like bash -c 'stat=$(echo "not running"); [[ $stat = "not running" ]] && echo yes – ilkkachu Nov 2 '17 at 18:12
  • @JeffSchaller, that only applies to languages where assignments work in arbitrary expressions, and for compilers that are dumb enough not to warn about it. – ilkkachu Nov 2 '17 at 18:13
  • please advice why this not works ?? - [[ "not running" =~ $STAT ]] or [[ "not running" =~ "$STAT" ]] – yael Nov 2 '17 at 18:21
  • It's still good practice to put the constant on the LHS, just so that you don't have to think about whether you're in a context where it matters or not. As to quotes, I may have been mistaken about needing them, but similarly, I tend to err on the side of "quote everything" out of an abundance of caution. – DopeGhoti Nov 2 '17 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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