If you need to pass a string like opt1=val1,opt2=val2,...
as a single argument to a command, but want to have it in multiple lines in the script for editability, you could use an array as an intermediate step and then join all the array elements when running the command.
E.g.
opts=(
opt1=val1 # no commas here
opt2=val2 # but comments also work
etc.
)
(IFS=,
echo somecmd --options "${opts[*]}" # echo for demonstration
)
That prints somecmd --options opt1=val1,opt2=val2,etc.
(The first pair of parenthesis is part of the array assignment syntax, the second starts a subshell to shield the rest of the script from the modified IFS
. That subshell might not be necessary, esp. if the script doesn't do anything afterwards, or if you just reset IFS
later or don't use it at all.)
Obviously that requires a feature-rich shell with support for arrays. In addition to Bash, zsh would be fine. In ksh, you'd need to quote the strings (or at least the equal sign), so it doesn't turn it into a struct-like compound variable.
If you require a POSIX sh compatible solution, the nearest equivalent to an array would be the list of positional parameters, but there's only one set of them, and since set
acts more or less like a regular command, you still need to remember the backslashes (and can't use comments).
Instead, something like this should work:
args=$( <<EOF sed -e 's/^[[:space:]]*//' | tr '\n' ,
opt1=val1
opt2=val2
etc.
EOF
)
args=${args%,}
echo somecmd --options "$args" # echo for demonstration