Contents
What is a sourced script?
Sourcing a script will run the commands in the current shell process. Executing a script will run the commands in a new shell process. Use source if you want the script to change the environment in your currently running shell. use execute otherwise.
What does the source command do?
The source command reads and executes commands from the file specified as its argument in the current shell environment. It is useful to load functions, variables, and configuration files into shell scripts. source is a shell built-in in Bash and other popular shells used in Linux and UNIX operating systems.
How is the source command used in a shell script?
The source command can be used to load any functions file into the current shell script or a command prompt. It read and execute commands from given FILENAME and return. The pathnames in $PATH are used to find the directory containing FILENAME. If any ARGUMENTS are supplied, they become the positional parameters when FILENAME is executed.
How to read a file using the source command in Bash?
With the source command, you can also read variables from a file. The variables must be set using the Bash syntax, VARIABLE=VALUE. Let’s create a test configuration file: In your bash script, use the source command to read the configuration file: If you run the script the output will look like this:
Can a source command pass arguments to another script?
In particular, if the source command does not specify any argument, it is assigned the ones from the calling environment. A consequence is that it may be troublesome to source a script passing no arguments within another script.
How to change the source of a bash script?
You can also change the source with a dot command which does the same thing but is supported in both bourne and bash. Now the script will work with either sh or bash. You should also run the script directly to avoid confusions like these by making it executable chmod +x xRUN, and invoking it like this: