Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-62657

Shell steps swallow ticks(') and quotes(") that are escaped

    Details

    • Type: Improvement
    • Status: Open (View Workflow)
    • Priority: Minor
    • Resolution: Unresolved
    • Labels:
      None
    • Environment:
      Jenkins version 2.138.1 but has been tested against the latest LTS and still happens
    • Similar Issues:

      Description

      I have an issue where I was trying to run a ssh command inside of a shell step but could not preserve the ticks and quotes I needed in the command I run during the SSH. The command looked something like this:

      ```
      sh "ssh -i $keyFile $adminUser@$Host 'sudo $command -f ${Cloud} -u ${env.USERNAME} -x ${env.PASSWORD} -s \"mountPoint=\'/admintool/1\'\" -p redeploy'"
      ```

      The issue is that the shell script I am running $command requires that mountPoint variable to be within quotes " and the argument after the equals needs to be in ticks '.

      This issue only happens on shell steps as I can echo the command correctly by replacing the above sh with echo.

      Workaround: In the end I had to build a shell script on disk and just run that so something in the groovy interpretation is breaking things.

      Steps to reproduce:
      1. Start up a brand new Jenkins instance with Pipeline installed
      2. Create a new job with the following definition:

      node(){
          echo """
          ssh -i $PEM_KEY $USER@$MACHINE 'echo "-f Cloud -u USERNAME -x PASSWORD -s \"mountPoint=\'/admintool/1\'\" -p redeploy"'
          """
      
          sh """
          ssh -i $PEM_KEY $USER@$MACHINE 'echo "-f Cloud -u USERNAME -x PASSWORD -s \"mountPoint=\'/admintool/1\'\" -p redeploy"'
          """
      }
      

      3. Observe that the echo prints out the statememt correctly with: `"mountPoint='/admintool/1'" but the ssh command echo does not as it removes the single ticks around admintool

      Thoughts:
      My understanding of echo is that it is almost the same as `sh` so I am not sure why the shell step is removing the escaped characters. Maybe there is some interpretation done on `sh` I am not aware of. So if there is any way to fix this or a better workaround I am all ears

        Attachments

          Activity

          Hide
          alexanderrtaylor Alex Taylor added a comment -

          Fixed it with `ssh -i $PEM_KEY $USER@$MACHINE 'echo "-f Cloud -u USERNAME -x PASSWORD -s \\\"mountPoint=\\\'/admintool/1\\\'\\\" -p redeploy"'` but that is pretty annoying and makes no sense

          Show
          alexanderrtaylor Alex Taylor added a comment - Fixed it with `ssh -i $PEM_KEY $USER@$MACHINE 'echo "-f Cloud -u USERNAME -x PASSWORD -s \\\"mountPoint=\\\'/admintool/1\\\'\\\" -p redeploy"'` but that is pretty annoying and makes no sense

            People

            • Assignee:
              Unassigned
              Reporter:
              ataylor Alex Taylor
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: