1

As a naive noob, I created several bash aliases intended to accept arguments, and they work exactly as intended. I tried to make a few more such aliases, however, and these would not work as intended.

This problem led me here, where I learned that bash aliases do not accept arguments. Instead, you must create a function. For example see here or here.

So, assuming the "bash aliases do not accept arguments" consensus is correct, why do all of these work?

alias pacss='pacman -Ss '
alias pacs='sudo pacman -S '
alias yausnc='yaourt -S --noconfirm '

When I say these "work" I mean that, e.g., typing 'pacs package' results in prompting me for password, prompting for confirmation, then retrieving and installing package.

So, why do these aliases work? Are the terms they accept not technically considered 'arguments'? Is it an idiosyncrasy of Arch (or Manjaro, which I am using)? A hole in the space-time continuum?

Any clarification shall be appreciated.

EDIT: Thanks for the answers explaining these are not calling for arguments. I don't quite understand how my first examples are different from the following attempted alias, which does not work:

alias lping='ping 192.168.1.'

This fails because ping executes without appending the number I type. My intent was to be able to enter e.g. lping 123 and have bash execute "ping 192.168.1.123" but instead it says "unknown host 192.168.1."

Community
  • 1
  • 1
  • 1
    Your aliases execute `pacman` or `yaourt`. Both accept arguments themselves. Any input given will be read by `pacman` or `yaourt` just as if they were executed on the command line. Your aliases need no arguments, but the programs (called by any name or alias) can accept arguments. Glad to see you are using Arch! – David C. Rankin Feb 06 '16 at 08:26
  • Those are `operations` and `options`... the target would be the `argument`. – l'L'l Feb 06 '16 at 08:30

1 Answers1

6

No, the terms they "accept" aren't called "arguments".
Bash aliases are just text replacements.
That is, when you write

pacs

bash replaces that with

sudo pacman -S 

and when you write

pacs package

bash replaces that with

sudo pacman -S package

In other words, the arguments aren't passed to the alias – the characters after the alias are just appended to whatever the alias expands to.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Thanks for the answer. "Appending the characters" after the alias is what I was hoping to accomplish with those (which worked), and also with the following, which does not work: alias lping='ping 192.168.1.' This always fails because it tries to ping 192.168.1. without appending the number I type on the cli after the alias. How is this different from the pacman aliases which do work? – idontcwutididthere Feb 07 '16 at 08:51
  • 1
    @idontcwutididthere The shell substitutes the aliased token with its definition and that's all... in particular, the shell doesn't join the expansion with the subsequent token on the command line, so when you write `lping 123` the command that is executed is `ping 192.168.1. 123` (note the space between `1.` and `123`). – gboffi Feb 07 '16 at 09:32
  • 1
    @idontcwutididthere The space between `lping` and the number is preserved. (The trailing space in your pacman aliases are unnecessary, by the way.) – molbdnilo Feb 07 '16 at 09:37