-2

~/.bashrc

My ~/.bashrc contains the following (by default):

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

~/.bash_aliases

My ~/.bash_aliases contains the following (my definition):

alias define="curl dict://dict.org/d:$1"

Expectation

The define in there is the alias so I could use something like the following directly in my shell (terminal):

define internet

While I expect it to look at dict.org and look up definition of internet and display it to me.

For some reason whenever I run it I get the following (hosted on paste.debian.net, might disappear in time).


I tried sourcing the ~/.bash_aliases in the bash session.


Shell

I'm running bash version 5.0.11 on Debian Bullseye (currently testing, and planned major upcoming release).


What am I doing wrong with my own alias in ~/.bash_aliases?

5
  • nope it doesn't . I have edited the question so it makes it more explicit that I am using define just as an alias and not as a function. Hope that makes it clearer above.
    – shirish
    Feb 12 '20 at 19:49
  • 2
    Use a function. Aliases can't take arguments, as you appear to try to do. The answers to the question that jesse_b linked to explains this in detail.
    – Kusalananda
    Feb 12 '20 at 19:51
  • So this example given is wrong ? read.cash/@Polydot/… @jesse_b
    – shirish
    Feb 12 '20 at 19:58
  • @shirish: Yes it is.
    – jesse_b
    Feb 12 '20 at 19:59
  • can you share how the function would look to fix it in .bashrc or should I make a newer question ?
    – shirish
    Feb 12 '20 at 20:00
4

The problem is that the alias doesn't take positional parameters like a script or function would. Additionally your alias in the bashrc is in double quotes so the parameter is expanded immediately (to nothing). So your alias is essentially being set to:

alias define="curl dict://dict.org/d:"

Even if you properly escaped the parameter either with single quotes or \ you would still not pass the argument in the way you would think.

define foo for example would result in curl dict://dict.org/d: foo, to use it that way you would need to set your argument as a shell argument using set:

set -- foo; define

However a function is definitely the way to go when arguments/options are needed:

define () {
    local word=$1
    curl "dict://dict.org/d:$word"
}
0
3

Aliases don't work that way.

The function is more powerful, and can be use:

function define(){
  curl "dict://dict.org/d:$1"
}

$1, is argument 1 of a script or function.

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