How to use the source command in Linux

Time:2021-10-27

The source command is a built-in shell command that reads and executes commands from files in the current shell session. The source command is usually used to keep and change environment variables in the current shell. In short, source is a script that will run the execute command in the current shell.
The source command can be used to:

Refresh the current shell environment
Execute shell script using source in current environment
Import a shell function in the environment from the script
Read variables from another shell script
Syntax of the source command
The source command, which requires a file, will be used as a location parameter for passing the script if a parameter is provided.

source FILENAME [ARGUMENTS]
You can also use. Instead of the source command:

. FILENAME [ARGUMENTS]
How to use the source command

  1. Refresh the current shell environment
    You can define an alias in the current shell environment. Define an alias for LS – Al as LL:

[[email protected] ~]# 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:

[[email protected] ~]# source ~/.bashrc
Now you can use the LL alias to list all files in the current directory, including hidden files.
How to use the source command in Linux how to use the source command in Linux

  1. Execute shell script using source in 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:

[[email protected] ~]# website=https://www.linuxprobe.com
How to use the source command in Linux how to use the source command in Linux
Create a script:

[[email protected] ~]# vim web.sh

!/bin/bash

echo $website
How to use the source command in Linux how to use the source command in Linux
Use source to execute it in the current shell session:

[[email protected] ~]# source web.sh
https://www.linuxprobe.com
How to use the source command in Linux how to use the source command in Linux
When running the script with SH, the variables defined in the environment cannot be found:
How to use the source command in Linux how to use the source command in Linux

  1. Import a shell function in the environment from the script
    First, create a script and define a function:

[[email protected] ~]# vim func.sh

!/bin/bash

foo(){
echo “test function!”
}
How to use the source command in Linux how to use the source command in Linux
To import the functions of the above script in the current shell session, use the following command:

[[email protected] ~]# source func.sh
Now try to execute the foo function in the following func.sh script in the terminal:

[[email protected] ~]# foo
test function!
How to use the source command in Linux how to use the source command in Linux
After the source script is completed, you can see that the functions in the script can be executed in the terminal of the current reply.

  1. Read variables from another shell script
    First create a shell script with some variables, enter:

[[email protected] ~]# vim var.sh

!/bin/bash

a=1
b=2
c=3
How to use the source command in Linux how to use the source command in Linux
Create another script, which reads the variables in the previous script var.sh:

[[email protected] ~]# vim read.sh

!/bin/bash

source ~/var.sh
echo $a
echo $b
echo $c
In the script, first use source to send the variable in var.sh to the current reply, and then echo displays the variable value. Now execute read.sh to see:
How to use the source command in Linux how to use the source command in Linux

  1. Read and execute commands
    The source command can read and execute commands from files. The following text file contains two commands. Run the file with the source command to see if the commands in it will be executed.

Next, create a file cmd.txt and save two commands:

[[email protected] ~]# cat cmd.txt
ip ad
date
Use source to execute this file:

[[email protected] ~]# source cmd.txt
How to use the source command in Linux how to use the source command in Linux

summary
The source command executes the script in the current shell, while the exec command runs in the new shell.