The source command is a built-in shell command used to read and execute commands from a file in the current shell session. The source command is usually used to preserve and change the environment variables in the current shell. In short, source a script that will run the execute command in the current shell. |
source
Commands can be used:
- Refresh the current shell environment
- Use source to execute shell scripts in the current environment
- Import a Shell function in the environment from the script
- Read variables from another shell script
Syntax of the source command
source
It requires a file for the command. If a parameter is provided, it will be used as the positional parameter of the passing script.
source FILENAME [ARGUMENTS]
.
Alternative source
commands can also be used :
. FILENAME [ARGUMENTS]
How to use the source command
1. Refresh the current shell environment
You can define an alias in the current shell environment. To ls -al
define an alias as ll
:
[root@localhost ~]# echo "alias ll='ls -al'" >> ~/.bashrc
After defining the alias in the ~/.bashrc file, you can use the source command to refresh the current shell environment:
[root@localhost ~]# source ~/.bashrc
Now you can use ll
aliases to list all files in the current directory, including hidden files.
2. Use source to execute Shell scripts in the current environment
The shell script does not know the variables you define in the current shell environment. The source command can be used to execute your shell script in the current session.
A variable is defined below:
[root@localhost ~]# website=https://www.linuxprobe.com
Create a script:
[root@localhost ~]# vim web.sh #!/bin/bash echo $website
Use source
to execute it in the current shell session:
[root@localhost ~]# source web.sh
https://www.linuxprobe.com
When running the script with sh, the variables defined in the environment cannot be found:
3. Import a Shell function in the environment from the script
First create a script and define a function:
[root@localhost ~]# vim func.sh #!/bin/bash foo(){ echo "test function!" }
To import the functions of the above script in the current shell session, use the following command:
[root@localhost ~]# source func.sh
Let's foo
try the following functions in the func.sh script in the terminal :
[root@localhost ~]# foo test function!
After source the script, you can see that the functions in the script can be executed in the terminal of the current call.
4. Read variables from another shell script
First create a shell script with some variables, please enter:
[root@localhost ~]# vim var.sh #!/bin/bash a=1 b=2 c=3
Create another script that reads var.sh
the variables in the previous script :
[root@localhost ~]# vim read.sh #!/bin/bash source ~/var.sh echo $a echo $b echo $c
In the script, first use the source to convert var.sh
the variables in the current session, and then echo to display the variable value. read.sh
Take a look at the execution below :
5. Read and execute commands
source
Commands can read and execute commands from files. The following text file contains two commands. Use the source command to run the file to see if the commands inside will be executed.
Create a file cmd.txt below and save two commands:
[root@localhost ~]# cat cmd.txt ip ad date
Use the following to source
execute this file:
[root@localhost ~]# source cmd.txt
to sum up
source
The command executes the script in the current shell, and the exec
command runs in the new shell.