The source command on Linux is a pretty popular function run by system administrators daily. Used to refresh the current shell environment , the source command can also be used in order to import functions into other bash scripts or to run scripts into the current shell environment. Before starting, it is important to have a complete understanding of what environment and shell variables are.
By default, on Linux, your system already owns a couple of environment variables to store various information such as the shell to use, your hostname or your current username. Environment variables are initialized by the system and are inherited by all processes on the system. As a consequence, when you are running a shell instance, you are to get the value of environment variables on your system.
On the other hand, shell variables are variables declared within the context of a shell instance and they are not shared with other shells or with child processes. On Linux, when you execute a script, it is most likely executed in a subshell.
The source command is used in order to evaluate the file passed as an argument in the context of the current execution. Note that when arguments are provided, they are set as positional parameters for the script specified. Note : the source documentation is located inside the bash documentation. One of the main reasons to use source is to refresh the current shell environment by running the bashrc file. As a reminder,. For the changes to be applied, run the source command with the.
In contrast if the script is run just as filename , then a separate subshell with a completely separate set of variables would be spawned to run the script. There is a subtle difference between executing a script by running.
If any arguments are supplied, they become the positional parameters when filename is executed. The other lines are the output. Run the commands sequentially, one command at a time:. Sometimes, when writing scripts to launch programs and things especially, I have come across cases where export doesn't seem to work right. In these cases, sometimes one must resort to using files themselves as global variables to pass information from one program to another.
Here is how that can be done. Simply checking for the existence of a file, as shown above, works great for globally passing around boolean conditions between programs and processes.
However, if you need to pass around more complicated variables, such as strings or numbers, you may need to do this by writing these values into the file. In such cases, you should use the file lock function, flock , to properly ensure inter-process synchronization. It is a type of process-safe ie: "inter-process" mutex primitive.
You can read about it here:. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Unix: What is the difference between source and export? Ask Question. Asked 8 years, 8 months ago. Active 4 months ago. Viewed 19k times. Is source command in this case different than export? Improve this question. William Pursell k 44 44 gold badges silver badges bronze badges. Lolly Lolly 30k 38 38 gold badges silver badges bronze badges.
Why don't you just try and find out? I would be very wary of source -ing a user-defined file in a shell script in a production environment. Related, and very helpful and more-detailed: askubuntu.
Add a comment.
0コメント