与 $1 类似,但收集所有输入,无论空格如何

发布于 2024-12-05 01:03:55 字数 265 浏览 1 评论 0原文

是否有类似于 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千と千尋 2024-12-12 01:03:55

尝试 $*$@

$* 所有位置参数,视为单个单词

$@$*相同,但每个参数都是带引号的字符串,即
参数原样传递,无需解释或扩展。

Try $* or $@.

$* All of the positional parameters, seen as a single word

$@ Same as $*, but each parameter is a quoted string, that is, the
parameters are passed on intact, without interpretation or expansion.

寂寞花火° 2024-12-12 01:03:55

通常,您只需将第一个参数引用为 "$1",包括引号。如果您想使用目录名称作为参数,并且该名称中包含空格,您通常会在命令行上引用它:

alias finder='cd "$1" && open .'

...

finder "/some/dir/with spaces/in its name"

这也适用于制表符补全,它可以为您转义空格。在这种特殊情况下,您可能还可以直接使用 open 命令。

但是,如果您希望 finder 别名将多个参数连接成一个字符串,并用空格分隔,这实际上会更困难。我尝试了使用 $*$@ 的一些可能性,但它们无法正常工作。为了进行测试,我使用自己的命令 echol,它将每个参数打印在单独的行上。

$ echol foo bar
foo
bar
$ alias e='echol "$*"'
$ e foo bar

foo
bar
$ alias e='eval echo \""$*"\"'
$ e foo bar
 foo bar

最后一个是我最接近的,但它增加了一个额外的前导空间。

我认为你最好只引用目录名称。

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:

alias finder='cd "$1" && open .'

...

finder "/some/dir/with spaces/in its name"

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 command echol, which prints each of its arguments on a separate line.

$ echol foo bar
foo
bar
$ alias e='echol "$*"'
$ e foo bar

foo
bar
$ alias e='eval echo \""$*"\"'
$ e foo bar
 foo bar

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文