How to use the source command in Linux

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.

sourceCommands 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

sourceIt 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 sourcecommands 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 -aldefine 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 llaliases 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

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

How to use the source command in Linux How to use the source command in Linux
Create a script:

[root@localhost ~]# vim web.sh 

#!/bin/bash
echo $website

How to use the source command in Linux How to use the source command in Linux
Use sourceto execute it in the current shell session:

[root@localhost ~]# 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

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!"
}

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:

[root@localhost ~]# source func.sh 

Let's footry the following functions in the func.sh script in the terminal :

[root@localhost ~]# foo
test function!

How to use the source command in Linux How to use the source command in Linux
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

How to use the source command in Linux How to use the source command in Linux
Create another script that reads var.shthe 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.shthe variables in the current session, and then echo to display the variable value. read.shTake a look at the execution below :
How to use the source command in Linux How to use the source command in Linux

5. Read and execute commands

sourceCommands 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 sourceexecute this file:

[root@localhost ~]# source cmd.txt 

How to use the source command in Linux How to use the source command in Linux

to sum up

sourceThe command executes the script in the current shell, and the execcommand runs in the new shell.

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/114776718