This article shows you how to pass arguments that have space characters in their values.
Passing several arguments to a function that are stored as a string may not work properly when you have space in it.
Why ? because bash transforms it explicitly as an array with the read function. See the string to array example
By default, this is defined by the IFS variable as the following three whitespace characters (space, tab and end of line).
Demo:
IFS=$' \t\n'
ARGS_AS_STRING="--longoption='hello nico' --longoption2='hello world'"
read -r -a ARGS <<< "$ARGS_AS_STRING"
echo there is ${#ARGS[*]} arguments
4
(IFS=$'\n'; echo "${ARGS[*]}")
--longoption='hello
nico'
--longoption2='hello
world'
If your arguments stored as a string contains a space, you should:
Example:
ARGS_AS_STRING_TAB_SEPARATED=$(echo "$ARGS_AS_STRING" | sed 's/ --/\t--/g')
IFS=$'\t' read -r -a ARGS <<< "$ARGS_AS_STRING_TAB_SEPARATED"
echo there is ${#ARGS[*]} arguments
2
(IFS=$'\n'; echo "${ARGS[*]}")
--longoption='hello nico'
--longoption2='hello world'