Can't run bash script in after_success: "[[: not found"

I’m trying to run a specific bash script using after_success. The reason is that I want to push changes to docker hub but do it only when the master branch is updated, not when the PR passes the tests.

This is a super simplified version in order to keep it as short as possible.

The folder structure is like this

|-- scripts
    |-- dockerhub.sh
|-- index.js
|-- Dockerfile
|-- package.json
|-- .travis.yml

.travis.yml

sudo: required

services:
  - docker

script:
  - npm run test

after_success:
  - ./scripts/dockerhub.sh

dockerhub.sh

#!/bin/sh

if [[ "$TRAVIS_BRANCH" != "master" ]]; then
  echo "We're not on the master branch!!."
  # This will analyze the current branch and react accordingly
  exit 0
fi

if [[ "$TRAVIS_BRANCH" == "master" ]]; then
  echo "We ARE on the master branch!!!."
  # This will analyze the current branch and react accordingly
  exit 0
fi

In the build process I see this:

./scripts/dockerhub.sh: 3: ./scripts/dockerhub.sh: [[: not found
./scripts/dockerhub.sh: 9: ./scripts/dockerhub.sh: [[: not found

If I change the dockerhub.sh to this:

#!/bin/sh

if [[ "$TRAVIS_BRANCH" != "master" ]]; then
  echo "We're not on the master branch!!."
  # This will analyze the current branch TheDFury and react accordingly
  exit 0
fi

I still get the first error ./scripts/dockerhub.sh: 3: ./scripts/dockerhub.sh: [[: not found

This is the latest build:
https://travis-ci.com/github/rhernandog/dockerhub-travis-push/builds/166637696

Hey @rhernando,

It seems like the build is passing now. Can you confirm this? If you’re still having issues please post back and I’ll be glad to help you.

Change the shebang to #!/bin/bash.
[[” is Bash-specific syntax. Even when /bin/sh is Bash, it emulates the traditional sh and doesn’t support extended syntax when it’s invoked as “sh”.

1 Like

@native-api

That change plus a few environmental variables made the trick.

Thank you kind sir!!!

Imprint