How to make a shell script execute it's commands as source commands without having to use "source scriptname.sh" expression to launch the script? Basically what I want is to type ./scriptname.sh and it's commands to act as if source was used to execute the shell-script.
|
None of the Unix shells allow you to mark a script as one that should always be sourced the way you're hoping. What I would do is create an
In bash, you could write it as a procedure:
|
|||
|
|
You can use a feature of Add the following to your
This causes the function to be called for every command executed. If it's the name of a file that isn't executable (otherwise we'd block legitimate Example: Create
Then:
|
|||||||||||
|
|
The first line needs to contain the path to the interpreter: Then make the script file executable: Now you can run |
|||||||||||||
|
. fileis equivalent tosource fileActually,sourceis a "bashism" (Bash specific) and the dot operator is "standard" (POSIX). – Daniel Andersson Dec 5 '12 at 17:24sourcecommand is originally from Bill Joy's csh. – Nicole Hamilton Dec 5 '12 at 17:50