Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the source command

Shulou Source: shulou.com Published: 2022-06-01 14:38:54 09月19日 Update

This article introduces the knowledge of "how to use the source command". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Syntax of the source command

The source command requires a file, and if an argument is provided, it will be used as the location parameter for passing the script.

Source FILENAME [ARGUMENTS]

It can also be used. Override 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:

[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

You can now use the ll alias to list all the files in the current directory, including hidden files.

two。 Execute Shell scripts using source 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.shemaking raceme Basihecho $website

Use source to execute it in the current shell session:

[root@localhost ~] # homepage of source web.sh website

When you run a script using sh, you cannot find the variables defined in the environment:

3. Import a Shell function in the environment from a script

First create a script and define a function:

[root@localhost ~] # vim function.shemaking Universe Backfoo () {echo "test function!"}

To import the functionality of the above script in the current shell session, use the following command:

[root@localhost ~] # source func.sh

Let's try executing the foo function in the following func.sh script in the terminal:

[root@localhost ~] # footest function!

After source finishes the script, you can see that the functions in the script can be executed in the terminal of the current reply.

4. Read variables from another Shell script

To start by creating a shell script with some variables, enter:

[root@localhost ~] # vim var.shrunken pound Binhaxxxxxxxxx

Create another script that reads the variables from the previous script var.sh:

[root@localhost ~] # vim read.shroud cards bind Bash source ~ / var.shecho $aecho $becho $c

In the script, you first use source to move the variables in var.sh to the current reply, and then echo displays the values of the variables. Let's execute read.sh to take a look:

5. Read and execute commands

The source command can read and execute commands from a file. The following text file contains two commands. Use the source command to run the file to see if the commands in it are executed.

Create a file, cmd.txt, and save two commands:

[root@localhost ~] # cat cmd.txtip addate

Use source to execute this file:

[root@localhost ~] # source cmd.txt "how to use the source command" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Script command file variable environment function alias facet two content parameter more knowledge terminal website guide run practical learned next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi macOS Redmi Shulou Information