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

How to use the Linux system source command

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about how to use the source command of the Linux system. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The source command is a built-in shell command whose main purpose is to read and execute commands in FileName in the current bash environment.

The 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] 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 aliases in ~ / .bashrc file, you can use the source command to refresh the current shell environment:

[root@localhost ~] # source ~ / .bashrc can now use the ll alias to list all 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:

3. 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 try 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.

4. 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 get the variables in var.sh to the current call, and then echo displays the variable values. 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.txt ip ad date use source to execute this file:

[root@localhost ~] # source cmd.txt

This is how the Linux system source command shared by Xiaobian is used. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report