A brief introduction to the method of executing shell command under Linux


There are two ways to execute shell command under Linux

  1. Execute shell commands in the current shell
  2. Generate a subshell in the current shell and execute shell commands in the subshell

 1. Execute the shell command in the current shell

The main idea is to input shell commands directly from the command line through interactive mode, and then execute them directly from the command line to give the results

2. Generate a subshell in the current shell and execute shell commands in the subshell

For example, we write the shell as a shell script to run. At this time, we will start a subshell to replace the current shell, and then execute the shell script, such as


read -p "please input your name:" name
echo "hello $name"
Add execution permission Chmod + X demo.sh  The implementation is as follows

This script function is very simple, that is, output a line, prompt the user to enter his name, and then output Hello XXX. I mainly want to talk about the implementation results and their analysis. Here’s a picture:

First of all, I started two shells. At this time, the PID numbers of the left and right shells are 2473 and 2505 respectively, and the two processes are forked out of a process called process ID 2465.

When implemented./demo.shAfter that, you will see that there are three bash processes with process IDs of 2473, 2505 and 2633. Among them, 2473 and 2505 correspond to the process IDs of the previous two shells respectively. There is an extra process with PID 2633. This process is actually what we call a subshell. Look at the process number of the parent process of the process with PID 2633. That is to say, a subshell is started from the shell process with PID 2473. The process ID number of the subshell is 2633. After we input warjiang, the script continues to execute. After execution, the subshell process exits.

At this time, when we check the process again, we will find that the process with PID 2633 is missing, that is, the previous subshell is missing.

At this point, I have a certain understanding of how to execute shell in the current terminal and how to start subshell to execute shell in the current terminal. The following will lead to the difference between the source command and the execution of shell script in the terminal.

The difference between source and Bash

Strictly speaking, my title is poisonous, because the source command is part of bash. What I’m trying to say here issource demo.shAndbash demo.shThe difference between them.

I believe that any codeframer with Linux development and operation and maintenance experience will configure environment variables such as JDK, etc., and you will remember that you have executed themsource ~/.bash.rcSo what is the source? Let’s put this problem aside, let’s look down first. Or the one above demo.sh Let’s compare itbash demo.sh (or execute Chmod + X first demo.sh Then execute/ demo.sh )Andsource demo.shThe difference between them.

The picture above is execution./demo.shThe next one is a screenshot ofsource demo.sh. Comparing the two images, it is obvious that the biggest difference between the two images is that the above image has one more subshell, that is, the process of PID 2633 process appearing and disappearing. Therefore, if the shell script is executed, the current command line will automatically generate a subprocess. After the script is executed, the subprocess will be automatically closed. However, the souce command does not generate a new child process, but reads and executes the shell script in the current terminal process.

So the biggest difference between source and Bash is that source does not need to start a new shell, but bash needs to start a new shell

Go back to the one abovesource ~/.bashrcIn fact, it is equivalent to executing in the current terminal~/.bashrc, and the content in the. Bashrc file is the statement about the export path part in, that is, to set the path variable and executesource ~/.bashrcLet the configured new environment variable path take effect in the current terminal

Source and (.)


This way, ~ /. Bash? U aliases Generally, the persistence of alias is used to record the user’s alias records. Intuitively, execution. ~/.bash_aliasesIt should be to set some variable aliases in aliases. So this one.What is it? Actually, it’s here.AndsourceThe function of is the same, which means that the script is executed in the current terminal, so that the current terminal can share the alias configuration information in the aliases

Recommended Today

Envoy announced alpha version of native support for windows

Author: sunjay Bhatia Since 2016, porting envoy to the windows platform has been an important part of the projectOne of the goalsToday, we are excited to announce the alpha version of envoy’s windows native support. The contributor community has been working hard to bring the rich features of envoy to windows, which is another step […]