If you don't mind losing the value of the positional parameters, you can use them to build the command gradually. This also lets you use conditionals.
set -- command --first-option
# We need a special option on Tuesdays
if [ "$(date +%u)" = 2 ]; then
set -- "$@" --tuesday
fi
set -- "$@" --last-option
# Finally we execute the command
"$@"
If you want to add options before the positional parameters, you might need to build the command backwards.
# We have a list of files in the positional parameters.
set -- --last-option "$@"
# We need a special option on Tuesdays
if [ "$(date +%u)" = 2 ]; then
set -- --tuesday "$@"
fi
set -- command --first-option "$@"
# Finally we execute the command
"$@"
If you need to preserve the positional parameters, put this code in a function: the positional parameters are restored when the function returns.
\
escapes the next character, which "hides" newlines but makes spaces significant). – geekosaur Mar 21 '11 at 15:27