与 $1 类似,但收集所有输入,无论空格如何
是否有类似于 $1 的东西,但它收集来自终端输入的所有输入,包括空白字符?这将用于收集可能有空格的粘贴目录路径 - 我需要整个字符串。
提前致谢
谢天谢地,我已经收到了第一个问题的答案。然而,在执行过程中,我无法让它发挥作用。这是我的代码。谁能解释我做错了什么?谢谢。
alias finder='cd $* && open .'
它返回分段返回 - 每次它遇到空格时,它都会将其视为一个单独的条目。
Is there something similar to $1, but that gathers all input from the terminal input, including whitespace characters? This would be used to collect a pasted directory path that may have whitespaces - I need the whole string.
Thanks In Advance
Thankfully, I've received the answer to my first question. In execution, however, I can't get it to work. Here is my code. Can anyone explain what I'm doing wrong? Thanks.
alias finder='cd $* && open .'
It's returning segmented returns - every time it hits a space, it treats it as a separate entry.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
$*
或$@
。Try
$*
or$@
.通常,您只需将第一个参数引用为
"$1"
,包括引号。如果您想使用目录名称作为参数,并且该名称中包含空格,您通常会在命令行上引用它:这也适用于制表符补全,它可以为您转义空格。在这种特殊情况下,您可能还可以直接使用
open
命令。但是,如果您希望 finder 别名将多个参数连接成一个字符串,并用空格分隔,这实际上会更困难。我尝试了使用
$*
和$@
的一些可能性,但它们无法正常工作。为了进行测试,我使用自己的命令echol
,它将每个参数打印在单独的行上。最后一个是我最接近的,但它增加了一个额外的前导空间。
我认为你最好只引用目录名称。
Normally you'd just refer to the first argument as
"$1"
, including the quotation marks. If you want to use a directory name as an argument, and the name has spaces in it, you'd typically quote it on the command line:That also works well with tab completion, which escapes whitespace for you. And in this particular case, you probably might as well use the
open
command directly.But if you want the
finder
alias to concatenate multiple arguments into a single string, separated by spaces, that actually turns out to be harder. I've tried some possibilities using$*
and$@
, but they don't work correctly. For testing, I'm using my own commandechol
, which prints each of its arguments on a separate line.That last one is the closest I've come, but it adds an extra leading space.
I think you're better off just quoting the directory name.