
How to execute .sh file in Linux system?
In Linux systems, the.shfile is a file called a Shell script, which is used to execute a series of commands. Executing the.shfile is a very common operation. This article will introduce how to execute the.shfile in the Linux system and provide specific code examples.
To execute a.shfile in the Linux system, you can use the absolute path to specify the location of the file. The following are the specific steps:
.shfile is stored../plus the file name to execute the.shfile, that is,./filename.sh, wherefilename. shis the name of your.shfile.Sample code:
cd /path/to/your/sh_file/folder ./example.sh
In addition to using the absolute path to execute.shfile, you can also add the.shfile to the PATH environment variable of the system so that it can be executed directly under any path. The following are the specific steps:
.bashrcfile. You can use the commandnano ~/.bashrcto open the file.export PATH=$PATH:/path/to/your/sh_file/folder, where/path/to/your/sh_file/folderis the path where you store the.shfile..bashrcfile, then run thesource ~/.bashrccommand for the changes to take effect..shfile in any path, just enter the file name.Sample code:
source ~/.bashrc example.sh
Sometimes when trying to execute.shInsufficient permissions may occur when accessing a file. In this case, you can use thechmodcommand to grant execution permissions to the file. The following are the specific steps:
.shfile is stored in the terminal.chmod x filename.shcommand, wherefilename.shis your.shfile name. This command will give the file execution permissions..shfile directly through the file name.Sample code:
cd /path/to/your/sh_file/folder chmod +x example.sh ./example.sh
Summary:
This article introduces three methods of executing.shfiles in Linux systems, and Specific code examples are provided. Through these methods, you can easily execute the.shfile and perform customized script operations. Hope this article helps you!
The above is the detailed content of How to execute .sh file in Linux system?. For more information, please follow other related articles on the PHP Chinese website!