Home> System Tutorial> LINUX> body text

The magic command in Linux: source

PHPz
Release: 2024-02-10 15:06:02
forward
1274 people have browsed it

Linux is a powerful and flexible operating system that provides many built-in shell commands that allow us to easily manage and control the behavior of the system. One very useful command is source, which allows us to read and execute commands from a file without creating a new process or environment. What does the source command do? How is it used? How is it different from bash commands? This article will introduce you to the secrets of the source command in detail, allowing you to harness the super power of Linux.

About the source command and its execution method: In Linux systems, source is a built-in Shell command that reads and executes the contents of the file in the current Shell. These files usually contain a series of commands to be read and run by the TCL interpreter.

If any arguments are provided, they will become positional arguments to [filename] that will be executed by source. Otherwise, the positional parameters remain unchanged.

The entry in $PATH is used to find the directory containing [filename], however, if the file does not exist in $PATH, it will search for the file in the current directory. The source command has no options, and the parameters can only be file names.

grammar:

source [filename] [arguments]
Copy after login

in:

[filename]: The name or path of the file to be executed by the source command.

[arguments]: When the file is executed, any arguments you provide will become positional arguments.

The dot (period) character can be substituted for the source command, producing the same output:

. [filename] [arguments]
Copy after login

Example:

Create a file called linuxmi.txt and paste the following commands into it.

echo “Hello Linux迷 www.linuxmi.com” pwd date echo $PATH
Copy after login

Now run the source command, using the correct path to the file you just created. Make sure you use the correct file path.

source linuxmi.txt

The above source command reads and executes the commands in the provided file.

The output is as follows:

Linux 中的神奇命令:source

Important Tips: Be careful! "./" and "source" are not exactly the same.

"./script" runs the script as an executable file, starting a new shell to run it. "source script" reads and executes the commands in the file from the current shell environment. Note: "./script" is not equal to ". script", but ". script" is equal to "source script".

How to use the source command to read the configuration file:

With the source command, we can create a configuration file and read it in another file.

Basically, we need to create two files:

sample-config.sh → Contains some random configuration

config-reader.sh → Read the configuration file and perform some operations.

Create these files in the same directory.

Create sample-config.sh and add the following commands:

profile_page="https://www.linuxmi.com/" article_list="https://linux.linuxmi.com/"
Copy after login

Create config-reader.sh and add the following commands:

#!/usr/bin/env bash source sample-config.sh echo "*****************************" echo "Profile Page : $profile_page" echo " List of Articles : $article_list" echo "*****************************"
Copy after login
Linux 中的神奇命令:source

The config-reader.sh file contains a source command that uses the configuration file. It uses the file variable from the config-reader.sh file in subsequent commands.

Output:

Execute the following command to read the configurations and use them in the current file source config-reader.sh

As shown below:

Linux 中的神奇命令:source

Refresh the current Shell environment:

Sometimes we want to set an alias for a frequently used command to make it easier to use in any shell session.

We can start by creating a Shell alias command like this:

alias ll=’ls -ltrh’

This command lists all files and folders in the current directory in long format, sorted by timestamp in ascending order, and displayed in an easy-to-read format.

Linux 中的神奇命令:source

Actually, there is a problem with the current command, it runs fine within the current shell session, but we cannot access it outside the current shell session.

However, we have a solution to make it a permanent command so that it can be accessed in any shell session.

We just need to add this alias to the .bashrc (or .zsh for macOS) file, which is located in the home directory.

sudo vim ~/.bashrc
Copy after login

Under the "#some more ls aliases" section, add the following:

alias ll = 'ls -ltrh'
Copy after login

我已经添加了类似的内容如下:

alias ll = 'ls -ltrh' alias g = 'git' alias k = 'kubectl'
Copy after login
Linux 中的神奇命令:source

添加别名后,我们需要刷新当前的Shell环境,以便测试它是否按预期工作。

source ~/.bashrc
Copy after login

通过本文,你应该对 source 命令有了更深入的了解,知道了它如何从文件中读取并执行命令,如何更新 bash shell 环境,以及它与 bash 命令的区别。source 命令是一个非常方便的内置 shell 命令,它可以让我们在同一个 shell 环境中修改变量或执行脚本,而不需要创建新的进程或环境。这样可以节省资源,提高效率,也可以避免一些潜在的错误或冲突。source 命令是 Linux 中的神奇命令之一,值得我们好好学习和使用。

The above is the detailed content of The magic command in Linux: source. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!