0

i am trying to get value of variable by given string: Running this code on Jenkins and its get "bad substitution" in regular shell it works. example:

param1="hello"
param2="world"
PARAMS="param1 param2"
for p in $PARAMS;do
        echo ${!p}" "
done

what the best way to make it work in Jenkins too.

  • 1
    Please confirm that you are using `bash` and not some other shell. I have a feeling that jenkins calls `sh` instead of `bash`. Add the command `ps` to the top of your script and show us the output. – Socowi Nov 23 '19 at 22:27
  • 1
    Further to @Socowi's remarks, add `#!/bin/bash` to the first line to force a bash shell. ps: works for my Jenkins instance, but I don't think it achieves what you expect; `${!p}" "` is the same effect as `"${p} "`. `${!p*}` is different. Odd [Parameter substitution](https://www.tldp.org/LDP/abs/html/parameter-substitution.html#PARAMSUBREF) – Ian W Nov 24 '19 at 00:07
  • i am using bash. i think the problem is that Jenkins try to parse/switch it and when it got ! it fail. – Matan Ramrazker Nov 24 '19 at 07:13
  • @IanW As far as I know the `#! ...` is ignored by jenkins. See [What shell does Jenkins use?](https://stackoverflow.com/a/12457930/6770384). Also, `${!p}" "` is **neither** the same as `"${p} "` **nor** `${!p*}` – OP is using *indirection*, see the official bash manual or [this site](https://mywiki.wooledge.org/BashFAQ/006#Evaluating_indirect.2Freference_variables). – Socowi Nov 24 '19 at 11:24
  • @MatanRamrazker Did you really confirm that you are using bash? I don't think so. Anyways, you could try to save your script in `/some/dir/myScript.sh`, prefix it with `#! /bin/bash`, make it executable, and only call `/some/dir/myScript.sh` in Jenkins. – Socowi Nov 24 '19 at 11:29

1 Answers1

0

You must use with $ sign before the name of the variable:

PARAMS="$param1 $param2"
for p in $PARAMS;do
        echo ${p}" "
done
Refael
  • 6,753
  • 9
  • 35
  • 54