In the manpage for the version of getopt that comes with Mac OSX, an example is given that uses the construction args=$(getopt optstring $*); set -- $args. What does set -- $args do here?Furthermore, consider the function and test stringf () {    args=$(getopt o: $*)    set -- $args    for i; do        echo $i    done}f -o 123 xyzIn Bash 3.2 and 4.3, this produces-o123--xyzbut in Zsh 5.1, it produces -o 123 -- xyzWhat causes the discrepancy? Is it because of Zsh's different word-splitting behavior, or something else? I tried different combinations of quoting and parameter expansion flags and couldn't really figure out what was going on under the hood in either shell.
What does the `set -- $args` line do here, and why does it behave differently between Zsh and Bash?
bash;zsh;array;arguments
set -- $args set the positional arguments base on content of $args. Now you will get the different behavior between zsh and other POSIX shells.Because zsh does not perform Field Splitting by default, you will get one string, which is content of$args. You must to call splitting explicitly to get the same behavior as bash (and also other POSIX shells):set -- ${=args}bash perform Field Splitting on content of $args, produce four string as you got. You can check $# to know number of positional arguments after set -- $args.Note that in case of bash, you should add set -f to turn of globbing as well.