I have been trying all day with no success to get bash to receive arguments: the closest reference to this I could find is:
How to pass parameters to an alias?
if i execute:
rename -v -n 's/^the.//' *
it does exactly what I need, but I would like to turn into into an alias that received "the." string at run time. Is there a way of doing this?
Please any ideas would be welcome!
I have tried this, but with no success:
alias rp="_rp(){ rename 's/"1ドル"//' *; unset -f _rp; }; _rp"
-
If you have a function, what do you need an alias for?glenn jackman– glenn jackman2017年08月15日 21:45:52 +00:00Commented Aug 15, 2017 at 21:45
1 Answer 1
You can't use arguments in an alias. (You can append items after it, but that then just complicates this situation.) Here's what the man page (man bash
) says about them:
The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. [...]
There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used. [...]
For almost every purpose, aliases are superseded by shell functions.
So, instead of an alias you should use a function.
rp() { rename "s{1ドル}{}" *; } # No "{}" characters in the substitution
Usage
rp 'the.' # Quotes optional but recommended. Remember . represents any character
-
Thanks for reply roaima. did not work for me. I entered the function into bash.rc, but when I run rp 'the.' doesn't work?user2804894– user28048942017年08月15日 21:35:46 +00:00Commented Aug 15, 2017 at 21:35
-
It worked! thank you roaima. the problem was I had not removed rp from my alias list. Works like a charm. Thanks!user2804894– user28048942017年08月15日 22:10:17 +00:00Commented Aug 15, 2017 at 22:10
-
As the fine manual tells you, aliases are essentially obsolete. Always use functions.icarus– icarus2017年08月15日 22:57:08 +00:00Commented Aug 15, 2017 at 22:57
-
@icarus ... except for really simple things like
alias ls="ls -F"
.2017年08月16日 11:56:49 +00:00Commented Aug 16, 2017 at 11:56 -
1@kusalananda no, much simpler to have an easy to remember rule like "always use functions" rather than "always use functions unless it is really simple" and then have to worry if something is simple enough.icarus– icarus2017年08月16日 19:56:46 +00:00Commented Aug 16, 2017 at 19:56