Get the App
SLTechnology News&Howtos  ›  Servers  › 

Introduction to the method of running shell Program in linux

Shulou Source: shulou.com Published: 2022-06-03 07:38:20 10月13日 Update

Today, the editor shares an introduction about how to run shell programs in linux. Maybe you are no stranger to the methods of running shell programs in linux, or you have never known the methods of running shell programs in linux. But don't worry, the editor will explain it in the simplest description today. Let's take a look.

First of all, let's look at a program called test.sh.

#! / bin/sh#this is a test.cd / tmpecho "hello,this is a test"

The method of executing the program:

1. Make the file executable and run the file directly.

The chmod command modifies the permissions of the file.

+ x gives the file executable permissions. Just like we run the program above.

2. Directly call the command interpreter to execute the program.

Since our interpreter is / bin/sh, we use the sh command interpreter to execute the program.

3. Use source to execute files.

Program execution difference description:

Of the three ways we run the shell program, the execution of the first two methods is as follows:

(1) the parent process receives the command and finds that it is not a built-in command, so it creates a shell process like itself to execute the external command.

(2) the shell subprocess replaces itself with / bin/sh, and the sh process sets its own runtime environment variables, including the $PWD variable.

(3) the sh process executes the built-in commands cd and echo in turn. During this process, the environment variable of the sh process (child process) is changed by the cd command.

(4) when the child process is finished, it dies, and the waiting parent process wakes up and continues to accept orders.

So we understand why the execution results of the first two methods are different from what we expected, and the current directory (environment variable) of the parent process cannot be changed by the child process.

However, a child process is not created when executing a shell script using source, but is executed directly in the parent process.

The above is a brief introduction to the methods of running shell programs in linux. Of course, the differences in the detailed use of the above have to be understood by everyone. If you want to know more, welcome to follow the industry information channel!

Tags: Processes programs commands methods runs files variables permissions environment interpreters procedures differences abbreviations strangers no including reasons that is differences Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS NVidia Shulou Tech Info Shulou Technology