Skip to content

Bash script template example and how to pass variables #2961

Answered by fiftin
mlanner asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @mlanner thank you for your detailed description of the problem.

Are you use docker image? I don't know how is it possible that bash script executed as root when your Semaphore run as semaphore without sudo.

By the arguments. Passing named variables as unnamed argument is not reliable.
You can use simple code to convert args to vars inside your script:

for a in "$@"; do
  if [[ "$a" =~ ^([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$ ]]; then
    key="${BASH_REMATCH[1]}"
    val="${BASH_REMATCH[2]}"
    declare "$key=$val"
  fi
done

echo $MY_ARG1
echo $MY_ARG2
./script.sh MY_ARG1=2 MY_ARG2=hello

I will add this to docs asap.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by fiftin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants