How to execute .sh file in Linux system?

PHPz
Release: 2024-03-14 18:42:03
Original
1378 people have browsed it

How to execute .sh file in Linux system?

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.

Method 1: Use the absolute path to execute the .sh file

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:

  1. Open the terminal and enter the directory where the.shfile is stored.
  2. Use./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
Copy after login

Method 2: Add the .sh file to the system PATH and execute it directly

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:

  1. Edit your.bashrcfile. You can use the commandnano ~/.bashrcto open the file.
  2. Add a line in the fileexport PATH=$PATH:/path/to/your/sh_file/folder, where/path/to/your/sh_file/folderis the path where you store the.shfile.
  3. Save and exit the.bashrcfile, then run thesource ~/.bashrccommand for the changes to take effect.
  4. Now you can directly execute the.shfile in any path, just enter the file name.

Sample code:

source ~/.bashrc example.sh
Copy after login

Method 3: Use the chmod command to give the .sh file execution permissions

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:

  1. Enter the directory where the.shfile is stored in the terminal.
  2. Run thechmod x filename.shcommand, wherefilename.shis your.shfile name. This command will give the file execution permissions.
  3. Then you can execute the.shfile directly through the file name.

Sample code:

cd /path/to/your/sh_file/folder chmod +x example.sh ./example.sh
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!