Shell script: can you tell if a script is called via an alias?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Shell script: can you tell if a script is called via an alias?
Say I have an executable shell script called "foo"...
and then I alias to it like so:
alias bar foo
so now if I run "foo" or "bar", the same shell script runs.
Is there a way, in the shell script, that I can tell if I was called as "foo" or "bar"? In tcsh, I was checking $0, but that always seems to be "foo" even if I ran it as "bar". I don't care if it's tcsh or bash or even a python script (preferably not perl or anything that can't be interpreted on the fly, like compiled C).
If you create a *link* 'bar' to 'foo', then when you run it, it can know it was called as 'bar' or 'foo' -at least in sh/bash. This method is used for rc.0/rc.6 shutdown items -rc.0 is a link to rc.6. The called name tells the script what to do(this is under Slackware -YMMV).
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094
Rep:
Quote:
Originally Posted by BrianK
Say I have an executable shell script called "foo"...
and then I alias to it like so:
alias bar foo
so now if I run "foo" or "bar", the same shell script runs.
Is there a way, in the shell script, that I can tell if I was called as "foo" or "bar"? In tcsh, I was checking $0, but that always seems to be "foo" even if I ran it as "bar". I don't care if it's tcsh or bash or even a python script (preferably not perl or anything that can't be interpreted on the fly, like compiled C).
The OP said: "Back to the drawing board", so the suggestion about using links is a way to implement what he seemed to be trying to do -differentiate between the way a single script 'identifies' itself.
After I replied, I remember that I had previously done this via symlinks as you suggested. That doesn't work in this particular case, however.
For the record, I'm writing a controller for a slew of applications - groups of which need their own, unique PATHs (and a few other env vars). Rather than polluting each user's env with a ton of potentially conflicting paths, the controller is setting paths on the fly, then launching the application. When the app closes, all env vars go back to what they were before.
The purpose of the alias is to send all the potential application executables through the controller script. Being that the aliases, themselves, are created on the fly, I've simply appended some extra info to each alias, i.e. "--prog_group foo --app_name bar". Problem solved.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.