从 Zsh 中的字符串末尾修剪空格 [英] Trimming whitespace from the ends of a string in Zsh

查看:24
本文介绍了从 Zsh 中的字符串末尾修剪空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不产生另一个进程的情况下从 Zsh 中的字符串末尾删除任何和所有空格?

How does one remove any and all whitespace from the ends of a string in Zsh without spawning another process?

查看Zsh 中的扩展文档(即部分14.3 参数扩展14.8.1 Glob Operators)—也可以通过 $ man zshexpn—我写了以下代码:

After looking at the documentation for expansion in Zsh (namely sections 14.3 Parameter Expansion and 14.8.1 Glob Operators)—also viewable via $ man zshexpn—I've written the following code:

${${var##[:space:]##}%%[:space:]##}

但 Zsh 似乎无法识别 [:space:] glob 运算符.根据我对文档中以下语句的理解,它应该:

But Zsh doesn't seem to recognize the [:space:] glob operator. As per my understanding of the following statement in the documentation, it should:

在下面讨论的需要模式的扩展中,模式的形式与用于文件名生成的形式相同;请参阅文件名生成.请注意,这些模式以及任何替换的替换文本本身都会受到参数扩展、命令替换和算术扩展的影响.

In the expansions discussed below that require a pattern, the form of the pattern is the same as that used for filename generation; see Filename Generation. Note that these patterns, along with the replacement text of any substitutions, are themselves subject to parameter expansion, command substitution, and arithmetic expansion.

这是 Zsh 中的错误还是我忽略了什么?

Is this a bug in Zsh or am I overlooking something?

现在,我只是使用 ${${var## ##}%% ##} 至少替换末尾的任何和所有空格字符.

For now, I'm just using ${${var## ##}%% ##} which at least substitutes any and all space characters at the ends.

我使用的是 Zsh 5.8.

I'm using Zsh 5.8.

推荐答案

引自 zsh Glob Operator 文档:

To quote from the zsh Glob Operator documentation:

请注意,方括号是对包含整个字符集的方括号的补充,因此要测试单个字母数字字符,您需要 ‘[[:alnum:]]’.

Note that the square brackets are additional to those enclosing the whole set of characters, so to test for a single alphanumeric character you need ‘[[:alnum:]]’.

所以只使用 [:space:] 而不是 [[:space:]] 就是你的问题.

So just using [:space:] instead of [[:space:]] is your problem.

有几种表达方式匹配先前事物的多个副本";在 zsh glob 模式中.您使用的一种需要打开 EXTENDED_GLOB 选项,是使用 x##,它匹配模式 x 的一次或多次出现(还有 x#,它匹配零次或多次出现的模式 x).另一个需要 KSH_GLOB 选项才能工作的是 +(x),它也匹配一个或多个 x(和 *(x) 匹配 0 个或更多.)这种样式的优点是可以与 kshbash 一起使用(后者需要 extglob 选项已启用),因此来自另一个 shell 的人可能更熟悉.

There are a few ways to say "Match multiple copies of the previous thing" in zsh glob patterns. One, which you used, requires the EXTENDED_GLOB option to be turned on, is to use x##, which matches one or more occurrences of the pattern x (There's also x#, which matches zero or more occurrences of the pattern x). Another, which needs the KSH_GLOB option to work, is +(x), which also matches one or more occurrences of x (And *(x) matches 0 or more.) This style has the advantage of working with ksh and bash (The latter needs the extglob option enabled) and thus might be more familiar to someone coming from another shell.

所以:

$ foo=$(printf " \t bar baz \t ") # Spaces and tabs before and after
$ printf ">%s<\n" "$foo"                               
>    bar baz     <
$ printf ">%s<\n" "${${foo##[[:space:]]##}%%[[:space:]]##}"
>bar baz<
$ setopt KSH_GLOB                                            
$ printf ">%s<\n" "${${foo##+([[:space:]])}%%+([[:space:]])}"
>bar baz<

根据需要删除所有前导和尾随空格字符.

removes all the leading and trailing whitespace characters like you want.

另请注意,这种扩展嵌套是 zsh 特定的.它不会在其他 shell 中工作,其中两次删除将需要多个步骤.

Also note this nesting of expansions is zsh-specific. It won't work in other shells, where the two deletions will require multiple steps.

这篇关于从 Zsh 中的字符串末尾修剪空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆