Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Application of Source Command in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "the application of Source commands in Linux". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the application of Source command in Linux".

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 typically used to retain 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 scripts using source in the current environment

Import a Shell function in the environment from a script

Read variables from another Shell script

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

one。 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.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 you run a script using sh, you cannot find the variables defined in the environment:

three。 Import a Shell function in the environment from a script

First create a script and define a function:

[root@localhost ~] # vim func.sh #! / bin/bash foo () {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 ~] # foo test 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.

four。 Read variables from another Shell script

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

[root@localhost ~] # vim var.sh #! / bin/bash axiom 1 baked 2 caterpillar 3

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

[root@localhost ~] # vim read.sh #! / bin/bash source ~ / var.sh echo $an echo $b echo $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:

five。 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.txt ip ad date

Use source to execute this file:

[root@localhost ~] # source cmd.txt

Total knot

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

At this point, I believe you have a deeper understanding of "the application of Source commands in Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report