0

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 Aug 15 '17 at 21:45
3

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 Aug 15 '17 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 Aug 15 '17 at 22:10
  • As the fine manual tells you, aliases are essentially obsolete. Always use functions. – icarus Aug 15 '17 at 22:57
  • @icarus ... except for really simple things like alias ls="ls -F". – Kusalananda Aug 16 '17 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 Aug 16 '17 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.