I have a script which when called directly, works correctly with the following combination of parameters:
--path=/path/to/a\ folder\ with\ spaces
--path='/path/to/a folder with spaces'
--path="/path/to/a folder with spaces"
but I am having difficulty passing parameters from a helper script to it.
I have tried numerous things but in each case it is as if my script receives the following: --path=/path/to/a folder with spaces
which confuses my script and it is as if /path/to/a
is the path (i.e. argument of --path) and folder
with
spaces
are additional arguments.
As a minimal example, none of the following produces the desired result (save the below to a desired working directory as ./so_example.sh
and chmod +x ./so_example.sh
):
#!/usr/bin/env bash
set -x
printf "%s\n" "$@"
echo "$@"
echo -e "$@"
printf "%s\n" $@
echo $@
echo -e $@
When calling it in each of the following ways:
./so_example.sh --path=/path/to/a\ folder\ with\ spaces
./so_example.sh --path='/path/to/a folder with spaces'
./so_example.sh --path="/path/to/a folder with spaces"
I get the following output:
+ printf '%s\n' '--path=/path/to/a folder with spaces'
--path=/path/to/a folder with spaces
+ echo '--path=/path/to/a folder with spaces'
--path=/path/to/a folder with spaces
+ echo -e '--path=/path/to/a folder with spaces'
--path=/path/to/a folder with spaces
+ printf '%s\n' --path=/path/to/a folder with spaces
--path=/path/to/a
folder
with
spaces
+ echo --path=/path/to/a folder with spaces
--path=/path/to/a folder with spaces
+ echo -e --path=/path/to/a folder with spaces
--path=/path/to/a folder with spaces
I hope to get one of the following output for the correct solution:
--path=/path/to/a\ folder\ with\ spaces`
--path='/path/to/a folder with spaces'`
--path="/path/to/a folder with spaces"`