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 source command of Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article editor for you a detailed introduction of "Linux source command how to use", the content is detailed, the steps are clear, the details are handled properly, I hope this "Linux source command how to use" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Linux common commands the source command is a built-in shell command that reads and executes commands from files in the current shell session.

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.shemaking raceme Basihecho $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 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 try 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.shimmer becho binBash source ~ / var.shecho $aecho $becho $c in the script first use source to send variables in var.sh to the current call, and then echo displays the value of the variable. 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

After reading this, the article "how to use the source command of Linux" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, please 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