1

Using Cygwin on Windows 10. In ~/.bashrc, I write:

test=hola
alias add_hello="echo $1; echo hello"
alias add_hello2="echo $test; echo hello"

which, when both are run, results in:

$add_hello hola
hello hola
$add_hello2
hola hello

Why would these be run in different order? I have tried unaliasing them and simply redefining them without using .bashrc but the issue is the same. Also, writing a script called "add_hello":

#!/bin/bash
echo $1; echo hello

will work:

$./add_hello hola
hola 
hello

2 Answers2

2

What do you expect $1 to be when you call it from bashrc, Your $1 is never printed because you mentioned that in an alias but not setting it. As its empty , echo is not printing it.(echo "'<nothing here> hello' aa")

-->alias add_hello="echo hello"
-->add_hello aa       #< this is same as echo "'hello' aa"
-->hello aa          

Better you should try bash function in bashrc file:

add_hello()
{
echo 'hello'
echo "$1"
}
P....
  • 17,421
  • 2
  • 32
  • 52
  • 1
    Oh I see now. I was expecting it to act like a script where $1 would refer to an "argument" to the alias but I see it doesn't work the same way. Thanks – Benjamin.Guerin Oct 23 '19 at 18:58
1

your first test add_hello hola result in

echo $1; echo hello holla
boly38
  • 1,806
  • 24
  • 29