5

I have the following bash script

if [[$NODE_NAME = "Node1"]]
then
    dir="../../test"
fi

that I use in Jenkins execute shell prompt.

It gives me an error saying Slave1 command not found.

I just want to check if the $NODE_NAME variable equals the value "Slave1". How do I do that in bash?

don_crissti
  • 79,330
  • 30
  • 216
  • 245
Chris Hansen
  • 151
  • 1
  • 1
  • 3

1 Answers1

7

The [[ operator (and its other half, ]]) is actually a keyword, and as such needs to have a space on either side. While you're there, double quote your variables in case they contain spaces or other unexpected punctuation:

if [[ "$NODE_NAME" == 'Node1' ]]
then
    dir="../../test"
fi

As for checking the variable's value, just echo it. Add this line above your if condition:

echo "NODE: $NODE_NAME"
terdon
  • 234,489
  • 66
  • 447
  • 667
roaima
  • 107,089
  • 14
  • 139
  • 261
  • 1
    Quoting any variable expansion is a good practice, just that, inside `[[ ]]` isn't strictly needed. –  Oct 31 '15 at 02:23
  • 2
    It *might* be necessary on the right-hand side, if you want to ensure strict string matching when the expansion contains pattern metacharacters such as `*`, `[`, or `?`. – chepner Oct 31 '15 at 19:01