How to define a function on one line
16,207
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%/*}" ; }
Author by
Steffen
Updated on June 26, 2022Comments
-
Steffen almost 2 years
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'