There are several ways to bash a shell script, and now make a summary. Suppose we write a good shell script with the file name hello.sh and the file location in/data/the shell directory and has execute permissions. Method One: Switch to the directory where the shell script is located (at this time, called the working directory) to execute the shell script: Copy the Code as follows: CD/data/Shell./hello.sh./means to execute hello.sh under the current working directory. If you do not add./,bash may respond to an error message that could not be found hello.sh. Because the current working directory (/data/shell) may not be the default search path for the executing program, that is, it is not in the contents of the environment variable pash. The Echo $PASH command is available to view the contents of the path. Now the/data/shell is not in the environment variable pash, so it must be added.before it can be executed. Method Two: Execute the Bash shell script in an absolute path: Copy the code code as follows:/data/shell/hello.sh method Three: Use bash or sh directly to execute bash Shell script: Copy code code as follows: CD/data/shellbash hello.sh or copy code code as follows: CD/data/Shellsh hello.sh Note that if executed in method three, you do not have to set the shell's execution permissions beforehand, or even write the first line in the shell file (specify the bash path). Because method three is performed by passing hello.sh as a parameter to the SH (bash) command. This is not hello.sh to execute itself, but is called by others to execute, so do not execute permissions. Then you don't have to specify the bash path nature or understanding Ah, hehe ... Method Four: Execute the Bash shell script in the current shell environment: Copy the Code as follows: CD/data/shell. hello.sh or copy code code as follows: CD/data/Shellsource hello.sh The first three ways to execute a shell script is to open a child shell environment in the current shell (called the parent Shell), which is executed in this child shell environment. When the shell script finishes executing, the child shell environment closes and then goes back to the parent shell. The method four is executed in the current shell.
4 ways to execute shell scripts in Linux summary