如何在一行上定义一个函数 [英] How to define a function on one line
本文介绍了如何在一行上定义一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ #* /} $ {1%/ *}
}
不能把它写成单一的班轮。这是我试过的:
rmv(){mv $ 2 / $ {1 ## * /} $ {1%/ *}}
如果我这样做了,我得到这个错误:
-bash:.bashrc:第1行:意外标记附近的语法错误`{mv'
解决方案
在Bash中, {
不会自动识别为特殊/独立的令牌从它周围发生的事情。因此,您需要 {
和 mv
之间的空格。
此外:
}
需要作为命令的开始;所以如果它不是自己的行,你需要;
来终止前面的命令。
rmv(){mv$ 2 / $ {1 ## * /}$ {1%/ *}; }
Often when moving files around, I need to do the opposite later. So in my .bashrc I included this working code:
rmv() {
mv $2/${1##*/} ${1%/*}
}
Now I wonder why I can't write this as a single liner. This is what I tried:
rmv() {mv $2/${1##*/} ${1%/*}}
If I do so, I get this error:
-bash: .bashrc: line 1: syntax error near unexpected token `{mv'
解决方案
In Bash, {
is not automatically recognized as a special/separate token from what's around it. So you need whitespace between {
and mv
.
Additionally:
}
needs to be the start of a command; so if it's not on its own line, you need;
to terminate the previous command.- It's a best practice to always use double-quotes around any parameter expansion, since otherwise you'll get bizarre behaviors when the parameters include whitespace or special characters.
So:
rmv() { mv "$2/${1##*/}" "${1%/*}" ; }
这篇关于如何在一行上定义一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文