简体   繁体   English

zsh:参数扩展插入引号

[英]zsh: parameter expansion inserting quotes

I'm having trouble during parameter expansion in zsh: It's enclosing my variable in quotes. 我在zsh中的参数扩展期间遇到了麻烦:它将我的变量括在引号中。

Here is my script. 这是我的剧本。 (Apologies for the noise, the only real important line is the last one with the find call, but I wanted to make sure I'm not hiding details of my code) (对于噪音道歉,唯一真正重要的一行是find调用的最后一行,但我想确保我没有隐藏我的代码的详细信息)

    #broken_links [-r|--recursive] [<path>]
    # find links whose targets don't exist and print them. If <path> is given, look
    # at that path for the links. Otherwise, the current directory is used is used.
    # If --recursive is specified, look recursively through path.
    broken_links () {
        recurse=
        search_path=$(pwd)

        while test $# != 0
        do
                case "$1" in
                        -r|--recursive)
                                recurse=t
                                ;;
                        *)
                                if test -d "$1"
                                then
                                        search_path="$1"
                                else
                                        echo "$1 not a valid path or option"
                                        return 1
                                fi
                                ;;
                esac
                shift
        done

      find $search_path ${recurse:--maxdepth 1} -type l ! -exec test -e {} \; -print
    }

Just to be clear, in the find line, I'd like this: if recurse is null, substitute -maxdepth 1 . 为了清楚-maxdepth 1 ,在find行中,我想这样:如果recurse为null,则替换-maxdepth 1 If recurse is set to t , substitute nothing (ie let find do it's normal recursive behavior). 如果recurse设置为t ,则不替换任何内容(即让我们发现它是正常的递归行为)。

It may be kind of weird to look at because, although this is just the ${name:-word} form, word actually starts with a hyphen. 看起来可能有点奇怪,因为尽管这只是${name:-word}形式,但实际上word以连字符开头。 (See more about this here http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion ) (详情请见http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion

Instead, what's happening is, if recurse is null, it substitutes "-maxdepth 1" (note the surrounding quotes) and if recurse is set, it's substituting "" . 相反,正在发生的事情是,如果recurse为null,它将替换"-maxdepth 1" (注意周围的引号),如果设置了recurse ,则替换为""

The exact error when not recursing is find: unknown predicate `-maxdepth 1' . find: unknown predicate `-maxdepth 1'时的确切错误是find: unknown predicate `-maxdepth 1' You can try this for yourself by just saying find "-maxdepth 1" for example. 你可以自己试试这个,例如find "-maxdepth 1" When we do want recursion, something odd is happening I can't quite explain, but the error is find `t': No such file or directory . 当我们想要递归时,发生了一些奇怪的事情,我无法解释,但错误是find `t': No such file or directory

Does anyone know how to make zsh not place quotes in this parameter expansion? 有谁知道如何让zsh不在这个参数扩展中放置引号? I believe that is my problem here. 我相信这是我的问题。

Thanks. 谢谢。

zsh isn't actually adding quotes around that, it just isn't doing word splitting on the results of the parameter expansion. zsh实际上并没有添加引号,它只是没有对参数扩展的结果进行单词拆分。 This is how it's documented to behave by default. 这是默认情况下记录的行为方式。 From the zshexpn man page near the beginning of the parameter expansion section: 参数扩展部分开头附近的zshexpn手册页:

Note  in  particular  the  fact that words of unquoted parameters are not 
automatically split on whitespace unless the option SH_WORD_SPLIT is set

So, you can either set that option by doing setopt sh_word_split , causing splitting to be done for all parameter expansions or you can explicitly request it for just that expansion by using: 因此,您可以通过执行setopt sh_word_split来设置该选项,从而导致对所有参数扩展执行拆分,或者您可以使用以下命令显式请求它以进行扩展:

${=recurse:--maxdepth 1}

Note the = sign as the first character inside the braces. 注意=符号作为大括号内的第一个字符。 This is also noted in the zshexpn man page, search for ${=spec} . 这也在zshexpn手册页中注明,搜索${=spec}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM