search
HomeSystem TutorialLINUXThe magic command in Linux: source

The magic command in Linux: source

Feb 10, 2024 pm 03:06 PM
linuxlinux tutoriallinux systemlinux commandshell scriptarrangementembeddedlinuxGetting started with linuxlinux learning

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]

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]

Example:

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

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

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/"

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 "*****************************"
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

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

alias ll = 'ls -ltrh'

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

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

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

source ~/.bashrc

通过本文,你应该对 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!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
Is it hard to learn Linux?Is it hard to learn Linux?Apr 18, 2025 am 12:23 AM

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

What is the salary of Linux administrator?What is the salary of Linux administrator?Apr 17, 2025 am 12:24 AM

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

What is the main purpose of Linux?What is the main purpose of Linux?Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Does the internet run on Linux?Does the internet run on Linux?Apr 14, 2025 am 12:03 AM

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

What are Linux operations?What are Linux operations?Apr 13, 2025 am 12:20 AM

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Boost Productivity with Custom Command Shortcuts Using Linux AliasesBoost Productivity with Custom Command Shortcuts Using Linux AliasesApr 12, 2025 am 11:43 AM

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

What is Linux actually good for?What is Linux actually good for?Apr 12, 2025 am 12:20 AM

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

Essential Tools and Frameworks for Mastering Ethical Hacking on LinuxEssential Tools and Frameworks for Mastering Ethical Hacking on LinuxApr 11, 2025 am 09:11 AM

Introduction: Securing the Digital Frontier with Linux-Based Ethical Hacking In our increasingly interconnected world, cybersecurity is paramount. Ethical hacking and penetration testing are vital for proactively identifying and mitigating vulnerabi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use