The "bad substitution" error in a Jenkins pipeline script typically occurs when you are using an unsupported or incorrect syntax for a shell variable expansion.

To resolve this error, you can try the following steps:

Check the syntax of your variable expansion. Make sure that you are using the correct syntax for the shell you are using (e.g., Bash, Sh). For example, if you are using Bash, you should use ${VAR} syntax instead of $VAR.

Check if any special characters are present in your variable. Certain characters such as $, }, { can cause issues in shell expansions. You can try escaping them using a backslash (\) or using single quotes (') to avoid interpretation by the shell.

Check if the variable you are trying to expand actually exists. Ensure that the variable has been declared and initialized before you try to expand it.

Try using double quotes (") around your variable expansion. Double quotes will allow you to expand the variable and also perform any necessary shell expansions such as tilde expansion or command substitution.

Here's an example of how you could use double quotes to fix a bad substitution error:

sh "echo \"The current build number is ${BUILD_NUMBER}\""

If none of these steps resolve the issue, it's possible that there may be an issue with the environment or the shell itself. In that case, you may need to consult the Jenkins documentation or seek help from the Jenkins community.