String Escaping in Jenkins

Sometimes, working with variables, especially ones containing special characters (like $), we need to be more careful. NOTE: If you want to test the following, you will need a machine with Docker and Jenkins installed. Let’s take for an example this pipeline in Jenkins: pipeline { agent any environment { PASS='Pa$$word' } stages { stage('Clean') { steps { sh 'docker container rm --force $(docker container ls -aq) || true' } } stage('Escape Test #1 - NOK') { steps { sh 'docker container run -d --name db1 -e MYSQL_ROOT_PASSWORD=Pa$$word mariadb' } } stage('Escape Test #2 - NOK') { steps { sh "docker container run -d --name db2 -e MYSQL_ROOT_PASSWORD=Pa\$\$word mariadb" } } stage('Escape Test #3 - OK') { steps { sh "docker container run -d --name db3 -e MYSQL_ROOT_PASSWORD='Pa\$\$word' mariadb" } } stage('Escape Test #4 - OK') { steps { sh 'docker container run -d --name db4 -e MYSQL_ROOT_PASSWORD=\'Pa\$\$word\' mariadb' } } stage('Escape Test #5 - OK') { steps { sh '''docker container run -d --name db5 -e MYSQL_ROOT_PASSWORD='Pa$$word' mariadb''' } } stage('Escape Test #6 - OK') { steps { sh 'docker container run -d --name db6 -e MYSQL_ROOT_PASSWORD=$PASS mariadb' } } } } NOTE: MYSQL_ROOT_PASSWORD in the above example can be replaced with MARIADB_ROOT_PASSWORD. ...

March 19, 2026 · 3 min · Dimitar Zahariev