How do I run a different Shell interpreter in a Pipeline Script?

Issue

  • Can I run a ksh script in a pipeline step?
  • Can I run a perl script in a pipeline step?

Environment

  • CloudBees Jenkins Enterprise

Resolution

By default, the system default shell will be run. In a Shell Step, multiple lines are accepted. It is therefore possible to use an interpreter selector such as #!/bin/ksh or #!/usr/bin/perl.

KSH Example:

node {
    sh "#!/bin/ksh \n" +
       "echo \"Hello from \$SHELL\""
}

Perl Example:

node {
    sh "#!/usr/bin/perl \n" +
       "print \"Hello from \$SHELL\";"
}

Have more questions?

4 Comments

  • 0
    Avatar
    Blaise Pabon

    I was hoping to find an example with zsh. I'll have to hope that it's the same as ksh.

  • 0
    Avatar
    Grzegorz Zięba

    sh "..." will allocate default shell and then execute script according to shabang

    But there is no way to change shell for 'sh' statement . And that's an issue.

  • 0
    Avatar
    Denys Digtiar

    Hi Grzegorz,

    The plugins that provide `sh` step should recognize the "Shell executable" setting in the Manage Jenkins > Configure System. Note that the setting will change the default shell for other project types as well.

  • 0
    Avatar
    Grzegorz Zięba

    Hi Denis,
    Thank you for responding but I'm taking here about overwriting global configuration for single 'sh' statement which is currently not passible.

Please sign in to leave a comment.