开发者

Something similar to $1 but gathers all input regardless of whitespace

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 .'
开发者_如何学Python

It's returning segmented returns - every time it hits a space, it treats it as a separate entry.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜