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"
1
  • If you have a function, what do you need an alias for? Commented Aug 15, 2017 at 21:45

1 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .