Jenkins sh steps with [[: not found error

ismail yenigül
2 min readMar 3, 2022

--

You can use shell script in Jenkinsfile in steps, for example.

stage('Update ENV') {

steps {
sh(label: 'Updating K8S_ENV_NAME variable', script:
'''
#!/bin/bash
set +xe
echo $SHELL
if [[ ! -z ${ENVLIST} ]]
then
echo "ENV LIST: $ENVLIST"
CLIENTS=$(echo $ENVLIST | sed 's/,/ /g')
for CLIENT in $CLIENTS
do
echo "client: $CLIENT"
# do some actions....
done
else
echo "ENVLIST is not set"
fi
}
}

In this example, we are checking the existence of ENVLIST variable to take some actions.
You think that we set #/bin/bash, so it is using bash shell. But unfortunately that is not true! Jenkins uses sh by default, and it is calling this file something like $ sh myscript.sh this is the reason why you get [[: not found

$ sh myscript.sh/bin/bashmyscript.sh: 4: [[: not foundENVLIST is not set

Luckily, Jenkins has an option to set shell executable path. Simply go to Manage Jenkins > Configure System > Shell > Shell executable and set your shell path (ie /bin/bash)

Ismail YENIGUL

Devops Engineer

--

--

ismail yenigül
ismail yenigül

Written by ismail yenigül

CKA/CKAD,AWS certified Freelancer DevOps Engineer

Responses (1)