How to get name of alias that invoked bash script
alias, bash, scripting
Solution
I imagine you already know this, but for the record the answer is: you need cooperation by the code implementing the alias.
alternate_name () {
MY_ALIAS_WAS=alternate_name real_name "$@"
}
or, if you really want to use the superseded alias syntax:
alias alternate_name="MY_ALIAS_WAS=alternate_name real_name"
...and then...
$ cat ~/bin/real_name
#!/bin/sh
echo $0, I was $MY_ALIAS_WAS, "$@"
Problem
`$0` expands to the name of the shell script. ``` $ cat ./sample-script #!/bin/bash echo $0 $ chmod 700 ./sample-script $ ./sample-script ./sample-script ``` If the shell script is invoked via a symbolic link, `$0` expands to its name: ``` $ ln -s ./sample-script symlinked-script $ ./symlinked-script ./symlinked-script ``` How could I get the name of an alias? Here `$0' expands again to the filename: ``` $ alias aliased-script=./sample-script $ aliased-script ./sample-script ```