Search code examples
bashdoublequotes

Bash -- Double quotes within Backtick


I'm using the following conditional in a bash script:

if `grep -q "${ACTION_LABEL} Action" "${OVERRIDE_ACTIONS}"`; then
  ....
fi

It basically works. Except if any part of the path in ${OVERRIDE_ACTIONS} contains a space, then the grep fails. How can I get this to work with paths containing spaces>


Solution

  • Remove the backticks:

    if grep -q "${ACTION_LABEL} Action" "${OVERRIDE_ACTIONS}"; then
        ...
    fi
    

    Why? grep will return a value of 0 if it succeeded in finding occurences for your pattern, or 1 if it failed. The body of the if statement will be executed, if the condition evaluates to 0.