How to execute .sh file in Linux system?

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

How to execute .sh file in Linux system?

How to execute .sh file in Linux system?

In Linux systems, the .sh file is a file called a Shell script, which is used to execute a series of commands. Executing the .sh file is a very common operation. This article will introduce how to execute the .sh file in the Linux system and provide specific code examples.

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

To execute a .sh file 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 .sh file is stored.
  2. Use ./ plus the file name to execute the .sh file, that is, ./filename.sh, where filename. sh is the name of your .sh file.

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 .sh file, you can also add the .sh file 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 .bashrc file. You can use the command nano ~/.bashrc to open the file.
  2. Add a line in the file export PATH=$PATH:/path/to/your/sh_file/folder, where /path/to/your/sh_file/folder is the path where you store the .sh file.
  3. Save and exit the .bashrc file, then run the source ~/.bashrc command for the changes to take effect.
  4. Now you can directly execute the .sh file 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 .sh Insufficient permissions may occur when accessing a file. In this case, you can use the chmod command to grant execution permissions to the file. The following are the specific steps:

  1. Enter the directory where the .sh file is stored in the terminal.
  2. Run the chmod x filename.sh command, where filename.sh is your .sh file name. This command will give the file execution permissions.
  3. Then you can execute the .sh file 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 .sh files in Linux systems, and Specific code examples are provided. Through these methods, you can easily execute the .sh file 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
Popular Tutorials
More>
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!