0

I'm debugging a sh script. What's the behavior of these lines of code?

SOME_VAR=$(env)

if [ ! -z ${SOME_VAR+x} ]; then
    echo "SOMETHING HAPPENED "
fi

What does +x mean in this case? It turns out that code works fine with bash, but no with sh.

EDIT: I'm working with the deployment of an Eclipse Che local instance like it's explained here.

I installed the Eclipse Che CLI that is a .sh script:

$ curl -sL https://raw.githubusercontent.com/eclipse/che/master/che.sh > /usr/local/bin/che
$ chmod +x /usr/local/bin/che

To make it work, I need to run it using bash:

$ bash che start

If I debug the script...

$ sh -x che start 

The script fails here:

get_list_of_che_system_environment_variables() {

    [...]

    if [ ! -z ${CHE_VARIABLES+x} ]; then
      env | grep CHE_ >> $DOCKER_ENV
      RETURN=$DOCKER_ENV
    fi

Something is not working. If you say the substitution should work in .sh too, what would be the problem?

JonDoe297
  • 1,601
  • 1
  • 15
  • 21

2 Answers2

2

${SOME_VAR+x} expands to the empty string if SOME_VAR is not set. If it is set (even to the empty string), it expands to x.

Since SOME_VAR was set on the previous line, it will always expand to x. This makes the if statement irrelevant, as the code will always output SOMETHING HAPPENED.

chepner
  • 497,756
  • 71
  • 530
  • 681
1

It is a form of parameter substitution which will yield x if SOME_VAR is set, or a null String if it isn't.

You can learn more about the different type of parameters substitution here.

Aaron
  • 24,009
  • 2
  • 33
  • 57