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

Source: Internet
Author: User
Tags aliases

There are two ways to execute shell commands under Linux

    1. Executing shell commands in the current shell
    2. Generates a subshell in the current shell, executing shell commands in Subshell

1. Executing shell commands in the current shell

The main thing is to enter the shell command interactively in the command line, and the command line executes the result directly. Like this:

2. Generate a subshell in the current shell, execute shell command in Subshell

For example, we run the shell as a shell script, and this time we start a subshell instead of the current shell, and then execute the shell script like

#demo. SH

#!/bin/bash
read-p "Please input your name:" Name
echo "Hello $name"
chmod +x demo.sh

The script function is very simple, that is, output a line of words, prompting the user to enter their own name, and then the script output Hello xxx. I would like to say the results of the implementation and analysis. First come to a picture:

First of all, I started with two shells, and this time the PID numbers for the two shells were 2473 and 2505, and both processes were fork from a process called process ID number 2465.

When executed ./demo.sh , you will see three bash processes, with process IDs of 2473, 2505, and 2633. of which 2473 and 2505 correspond to the process IDs of the previous two shells respectively. More than one PID for the 2633 process. This process is actually the subshell we are talking about. The process number of the parent process for this PID 2633 process is 2473, which means that the process ID number of a Subshell,subshell from the shell process of PID 2473 is 2633. After we enter the Warjiang, the script continues to execute, and after the execution completes, the Subshell process exits.

This time when we go to see the process, we will find that the process of 2633 less PID, that is, less the front of the Subshell.

In this case, there is a certain degree of understanding of executing the shell in the current terminal with the execution shell two ways of launching the Subshell execution shell in the current terminal. The difference between the source command and the execution of the shell script in the terminal is drawn below.

The difference between source and bash

Strictly speaking, my title is poisonous because the source command was originally part of Bash. What I really want to say here is the source demo.sh bash demo.sh difference.

I believe everyone has Linux under the development, operational experience Codeframer will configure what JDK and so on environment variables, we will all remember that they have been executed source ~/.bash.rc , So what is this source? Let's put this problem first, let's look down. or the demo.sh script above. Let's compare bash demo.sh(或者先执行chmod +x demo.sh 再执行./demo.sh) source demo.sh the difference.

One of the above pictures is a screenshot of the execution ./demo.sh , and one of the following is source demo.sh . Compare two pictures, obviously, the biggest difference between the two pictures is that the above one picture more than a subshell is also the PID 2633 process of the emergence of the process of disappearing. So if you execute a shell script, the current command line automatically generates a subprocess, and when you finish executing the script, the child process is automatically closed. However, the souce command does not produce a new subprocess, but instead reads and executes the shell script in the current terminal process.

So the biggest difference between source and bash is that source doesn't have to start a new shell, and bash needs to start a new shell.

And then back to the top source ~/.bashrc , which is actually equivalent to executing in the current terminal, ~/.bashrc and the. bashrc file is in the statement about the Export Path section, which is to set the path variable and execute source ~/.bashrc the new environment variable path for the configuration to take effect in the current terminal.

Source and (.)

This way, ~/.bash_aliases generally uses the persistence of Alias to record the alias record of the user. Intuitively, execution . ~/.bash_aliases should be to set the variable alias in some aliases. So . what is this. In fact, this is the . same as source the role is to execute the script in the current terminal, so that the current terminal shares aliases in the alias configuration information

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.