Variable expansion is different in zsh from that in bash

后端 未结 1 871
孤独总比滥情好
孤独总比滥情好 2020-12-03 03:45

The following is a simple test case for what I want to illustrate.

In bash,

# define the function f
f () { ls $args; }

# Runs the c         


        
相关标签:
1条回答
  • 2020-12-03 04:35

    The difference is that (by default) zsh does not do word splitting for unquoted parameter expansions.

    You can enable “normal” word splitting by setting the SH_WORD_SPLIT option or by using the = flag on an individual expansion:

    ls ${=args}
    

    or

    setopt SH_WORD_SPLIT
    ls $args
    

    If your target shells support arrays (ksh, bash, zsh), then you may be better off using an array:

    args=(-a -l)
    ls "${args[@]}"
    

    From the zsh FAQ:

    • 2.1: Differences from sh and ksh

      The classic difference is word splitting, discussed in question 3.1; this catches out very many beginning zsh users.

    • 3.1: Why does $var where var="foo bar" not do what I expect? is the FAQ that covers this question.

    From the zsh Manual:

    • 14.3 Parameter Expansion

      Note in particular the fact that words of unquoted parameters are not automatically split on whitespace unless the option SH_WORD_SPLIT is set; see references to this option below for more details. This is an important difference from other shells.

    • SH_WORD_SPLIT

      Causes field splitting to be performed on unquoted parameter expansions.

    0 讨论(0)
提交回复
热议问题