Getting unknown long flag error while passing flags optionally

zsh has a number of significant differences from most Bourne-ish shells. You’re running into one of the biggest differences: by default it doesn’t split the result of variable expansions into separate “words”. In this case, that means that the expansion of ${DOWN_MIGRATE:+--migrate-only down $NO_OF_DOWN_MIGRATIONS} is passed as a single long argument, rather than being split into three words (“--migrate-only“, “down“, and “2“). The single long argument confuses the script.

You can tell zsh that you want word-splitting done by adding the = modifier to the expansion:

./scripts abc.sh ${=DOWN_MIGRATE:+--migrate-only down $NO_OF_DOWN_MIGRATIONS}

Note that if $NO_OF_DOWN_MIGRATIONS expanded to multiple words, that’d also be split. If you want to avoid that possibility (but keep the splitting of “--migrate-only” and “down“), you can double-quote just that part:

./scripts abc.sh ${=DOWN_MIGRATE:+--migrate-only down "$NO_OF_DOWN_MIGRATIONS"}

Anytime you take code samples intended for bash or other POSIX shells and put them in a zsh script, you need to be aware that zsh may do things differently from what the code assumes.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top