Several ways to execute scripts under linux

888u

Last update at :2024-05-13,Edit by888u

Prerequisite, the script must have execution permission. If not, execute the following command first:

chmod +x a.sh

1. Switch to the directory where the shell script is located (at this time, called the working directory) to execute the shell script:

cd /data/shell ./a.sh

./ means to execute a.sh in the current working directory. If you do not add ./, bash may respond with an error message that a.sh cannot be found. Because the current working directory (/data/shell) may not be among the default search paths of the executing program, that is, it is not among the contents of the environment variable PASH. To view the contents of PATH, use the echo $PASH command. The current /data/shell is not in the environment variable PASH, so ./ must be added before it can be executed.

Second, execute the bash shell script with an absolute path:

/data/shell/a.sh

Third, use bash or sh directly to execute the bash shell script:

cd /data/shell bash a.sh

or:

cd /data/shell sh a.sh

Note: If you execute method 3, you don’t need to set the execution permission of the shell in advance, and you don’t even need to write the first line in the shell file (specify the bash path). Because method three is to pass a.sh as a parameter to the sh (bash) command for execution. At this time, a.sh is not executed by itself, but is called and executed by others, so do not execute permissions.

Four, execute the bash shell script in the current shell environment:

cd /data/shell .a.sh

or:

cd /data/shell source a.sh

When executing shell scripts in the first three methods, a subshell environment is opened in the current shell (called the parent shell), and the shell script is executed in this subshell environment. After the shell script is executed, the subshell environment is closed and then returned to the parent shell. Method 4 is executed in the current shell.

Recommended site searches: server purchase, free virtual host in the United States, virtual host Hong Kong host, Chongqing virtual space, 6 yuan per year cloud server, website registration system, server rental, overseas virtual host space, domain name registration query, Korean server Rent,

Several ways to execute scripts under linux

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码