How To Run the .sh File Shell Script In Linux / UNIX

Ihave downloaded software for my Linux or Unix-like system from the Internet. There is a file called install.sh. How do I run an .sh file to install the software? How do your run .sh files from command line?

You can open or run .sh file in the terminal on Linux or Unix-like system. The .sh file is nothing but the shell script to install given application or to perform other tasks under Linux and UNIX like operating systems.The easiest way to run .sh shell script in Linux or UNIX is to type the following commands. Open the terminal (your shell prompt) and type the command.

How do I run .sh files in Linux?

The procedure to run the .sh file shell script on Linux is as follows:

  1. Set execute permission on your script:
    chmod +x script-name-here.sh
  2. To run your script, enter:
    ./script-name-here.sh
    OR
    sh script-name-here.sh
    OR
    bash script-name-here.sh

Let us see examples and usage in details.

Syntax

The syntax is:
sh file.sh
OR
bash file.sh

How to run .sh file as root user

Some time you need root access to install application; without root, you won’t have the necessary permissions to install application or make system level modifications. Root access is disabled by default on many Linux and UNIX like systems. Simply use the sudo or su command as follows:
sudo bash filename.sh
Type your password. Another option is to use the su command as follows to become superuser:
su -
Type root user password and finally run your script:
bash filename.sh

How to use chmod command to run .sh shell script in Linux

Another recommend option is to set an executable permission using the chmod command as follows:
chmod +x file.sh
Now your can run your .sh file as follows
./file.sh
/path/to/file.sh

How do I run .sh file named install.sh?

Just run the following two command:
chmod +x install.sh
./install.sh

The dot (.) refers to the current working directory. The install.sh is in the current directory so you run it as above or as follows too:
bash install.sh
OR
sh install.sh

GUI method to run .sh file

  1. Select the file using mouse.
  2. Right-click on the file.
  3. Choose Properties:
  4. Click Permissions tab.
  5. Select Allow executing file as a program:
  6. Now click the file name and you will be prompted. Select “Run in the terminal” and it will get executed in the terminal.

See also:

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.