Why is this something does Bash say that modavar command even tho i aliased it

Viewed 51

Note this is sourced, so this is not a shell script.

I am not asking for how to enable alias in noninteractive shell. I did this: shopt -s expand_aliases.

The Bash version is 5.1.4.

How to recreate:

Create a file named "p":

linky(){
comdll="cat"
shopt -s expand_aliases
alias modavar="$comdll"
echo "$argin" | modavar #| getlinks "$argin" | sort -u

}

Then run

echo "source p ; linky https://duckduckgo.com" | bash --norc

Expected output:

https://duckduckgo.com

Actual output:

p: line 5: modavar: command not found

When I run this once it give me

linky(){ alias jkl=echo\ hel; jkl; }
linky
bash: jkl: command not found

But if I do this,

linky(){ alias jkl=echo\ hel; jkl; }
linky
linky(){ alias jkl=echo\ hel; jkl; }
linky

It gives me

hel

What is happening?

1 Answers

You can't define the alias inside a function and use it there. Consider these examples:

alias foo=cat
ffoo() {
  echo abc|foo
}
fbar() {
  alias bar=cat
  echo abc|bar
}
ffoo  #->prints abc
fbar  #->prints command not found
Related