EDIT: Question was marked as a duplicate, please observe that value is sent in a specific way with double quotes like "--parameter value" "--parameter2 value2"
this question has not been asked and does not have an answer.
Problem
If the parameters are given double quoted like "--parameter value" "--parameter2 value2"
, how can I pass them so that an end script can take it as --parameter value --parameter2 value
?
If I try $@
(without double quotes) it only works if parameter values do not contain whitespaces. If I try "$@"
it does not work at all. Please see the scenario below that explains the problem with three files.
Real life scenario
- GitHub action (
action.yml
) calls aDockerfile
with double quoted parameter name and value pairs"--parameter value" "--parameter2 value2"
Dockerfile
in its entry point calls a shell script expecting parameters as--parameter value --parameter2 value2
(without double quotes per parameter name / value pair).
Simplified scenario
There are three steps in the call flow: the caller.sh
, middleman.sh
and script.sh
. And I'm only in control of the middleman
.
Caller
The entrypoint script. It calls the middleman
in specific way where each parameter name and value is double quoted together. I cannot control the behavior of this script.
So we could represent it with caller.sh
that would look like this:
#!/usr/bin/env bash
./middleman.sh "--parameter1 value" "--parameter2 'value with whitespace'"
Middleman
This is the middle layer that's supposed to take the parameters from the caller
and translates them into expanded parameters as the script
would expect.
middleman.sh
:
#!/usr/bin/env bash
./script.sh "$@"
This is where the problem lies as this does not work. I tried also with $@
without double quotes but then it only works if parameter values contain white space. How can I have the right call here?
Script
The script that's supposed to take the parameters in a right way. It wants to receive parameters such as --name value
.
It works by parsing the parameters as following and e.g. prints them:
script.sh
:
#!/usr/bin/env bash
while [[ "$#" -gt 0 ]]; do case $1 in
--parameter1) FIRST_PARAM="$2"; shift;;
--parameter2) SECOND_PARAM="$2"; shift;;
*) echo "[script.sh] Unknown parameter passed: '$1'"; exit 1;;
esac; shift; done
echo "[script.sh] parameter1: $FIRST_PARAM, second parameter: $SECOND_PARAM"
I cannot modify this script.
Run the test
./caller.sh