2

I'm having some difficulty understanding a subtlety of how a bash script works with arguments when it's accessed via a symbolic link. The issue is most easily explained with commands and their output:

$ type ll
ll is aliased to `ls -Alt --color=auto'

$ cat myshellscript
#!/bin/bash
type $1

$ ls -l myshellscript
-rwxr-xr-x 1 pi pi 20 Aug 22 19:55 myshellscript

$ . myshellscript ll
ll is aliased to `ls -Alt --color=auto'

$ ls -l /usr/local/bin
total 0
lrwxrwxrwx 1 root root 21 Aug 22 19:56 mss -> /home/pi/sh/myshellscript

$ mss ls
ls is /bin/ls

$ echo "OK, that worked"
OK, that worked

$ mss ll
/usr/local/bin/st: line 2: type: ll: not found

$ echo "Why didn't that work?"
Why didn't that work?
terdon
  • 242,166
jack
  • 23

1 Answers1

4

Expansion of aliases is disabled by default inside scripts: see for example

When you execute

. myshellscript ll

you are not just running myshellscript, you are sourcing it into the current (interactive) shell, where the alias is expanded. In contrast, when you run

mss ls

you are simply running the script, and alias expansion does not occur - as expected. You would see the same result if you had run ./myshellscript ll instead of . myshellscript ll

steeldriver
  • 81,074
  • The use of expanded here is confusing to me, as I would expect that to mean . myshellscript ll literally turns into . myshellscript ls -Alt --color=auto. But as I understand it, ll is simply not defined in the child process created by ./myshellscript ll. – jack Aug 24 '19 at 04:45
  • 1
    @Jonathan "Expanded" might not be the clearest way to state it. If ll were used as a command in the script, it would not be expanded as an alias; because of this, type ignores the fact that there's an alias by that name and reports that there's no (relevant) command by that name. – Gordon Davisson Aug 24 '19 at 06:12