HowTo: Run the .sh File Shell Script In Linux / UNIX

by Vivek Gite on September 26, 2007 · 28 comments

I've downloaded a software for Linux from the Internet. There is a file called install.sh. How do I run an .sh file to install the software?

.sh file is nothing but the shell script to install given application or to perform other tasks under 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:

sh file.sh

OR

bash file.sh

.sh File As Root User

Sometime you need to install application which requires root level privalage
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 sudo or su 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

chmod Command: Run 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

See also:

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 28 comments… read them below or add one }

1 lekhraj October 1, 2007

anybody tell by which coomand i will check the modified date of files/dir in linux o/s

Reply

2 rashid February 20, 2008

if we want to install software from cd.. how should be the command? i’m the newbie

Reply

3 anonomys December 19, 2008

rashid, if you want to install software from the cd you should try synaptic package handler or apt-get .

if the software is usually from the cd it will tell you to insert it

Reply

4 Black_Ps` June 11, 2009

How can i execute this command export PS1=”\[[33[01;32m\]\u@\h\[33[01;34m\] \W]\]#” from a .sh script for example and make it work ?

Thanks.

Reply

5 J_Art June 21, 2009

Hey Black_Ps`
There are a few ways to do this but if you are not familiar with some of the nuances of Linux, it might be easier to create a new folder to place the file in (or all of your scripts in) like home/username/Desktop/scripts (substituting the actual user name in place of username). The following instructions are based on Fedora 10 with GNOME desktop so if you are running a different Linux distro, you may or may not have a few slight variances. Once you have a good place to store your scripts, you can use the GUI to help with creation of the shell script. Next, click on the “Applications” menu and highlight “Accessories” and then select “Text Editor” Type the following command #!/bin/bash and then press the enter key. Now, type the command you listed into the text editor and then select the “Save As” option. Now name your file (highly recommended that you don’t name it with spaces and make the name all lower case to help with ease of use later) and save it to your new folder you created…remember to give it a .sh name extension. Now close the text editor and go back to the “Applications” menu and highlight “System Tools” and then select “Terminal”. Type the following command cd /home/username/Desktop/scripts (again substituting the actual user name in place of username). The command ls and then press the enter key. Did it return the name of your script as a file in that folder? If so, move on to the next step. If it did not, navigate to the folder where you saved this file and move it to the correct folder. Type the following command chmod 755 yourfilename.sh. That’s it, you now have an executable shell script that will execute your command. You can do so by double clicking on it from the GUI, calling it from the terminal like so bash /home/username/Desktop/scripts/yourfilename.sh or you could put it on the cron to run unattended if you needed to. I hope this helps some or at least points you in the right direction.

Reply

6 kashif February 9, 2012

can you please expalin it more detailes.

Reply

7 Black_Ps` June 21, 2009

Ok thanks i’ll give it a try

Reply

8 shunan June 30, 2009

When i open the terminal in UNIX..something like interactive keyboard authentication bla.. bla.. is coming..nd its difficult me to go on typing my commands and navigating through the directors and files..how can i fix this..? any idea…? any command? anything helpful will be highly appreciated. _thanks_

Reply

9 J_Art June 30, 2009

Hi shunan

Is it safe to assume that you have someone in an administrative role who oversees the security of this UNIX terminal? The reason I ask is because this sounds like something I encountered once with something called PAM (Pluggable Authentication Module). It’s a security concept that involves VSHELL. To be perfectly honest, I’m not too familiar with it and the only thing I can vaguely remember about it is that it somehow involves configuring authentication to be controlled by PAM via keyboard-interactive authentication. I realize I didn’t help much here but I hope this at least points you in the right direction. PAM is a product of SUN so you might find something worth while on their page but I haven’t really read up on it so I can’t make any promises. –> http://www.sun.com/software/solaris/pam/

Reply

10 Nate September 23, 2009

This is just straight pissing me off.
I’ve run the install.sh file in the terminal and have gotten to a eula where it asks if I accept and to type yes or no. I type yes and hit enter.
it said it can’t install it because permission is denied. But a window didn’t pop up to ask for my password.
I’m about to kick Ubuntu in the face.

Reply

11 Vivek Gite September 23, 2009

Try
sudo sh install.sh

Reply

12 Dare_devil December 2, 2009

hi i am newbie too to linux system i’ve a direct question:
i have arch linux but nothing from these commands work terminal tell me the following:
command not found
help me!

Reply

13 tugrul March 2, 2010

if you are using fedora 12 like i am, open up the terminat, switch to the root and with the terminal get on the fileway that you downloaded your .sh file.So write “sh YOURDOWNLOADEDFILE.sh” click on enter and that’s it

Reply

14 Gary James March 8, 2010

Excellent advice. Thanks Gary ( A newly converted Linux enthusiast)

Reply

15 dishant March 23, 2010

can i execute a shell script in another shell script

Reply

16 Iqbal June 5, 2010

Hey all,

Yes dishant,

You can run the shell script inside the other.
But keep in mind that In the second script do not kill the first one.
If you want to kill the first one, then back up the file name somewhere and kill it, then use that path in the second script.

Cheers,
Iqbal S.

Reply

17 panchotiya jigar r April 15, 2010

this is the best site of linux information to me
i learn lots of by this web site
it’s realy very helpful web site
i use this website for longtime
it’s give all answer of your questions
thanks for help

Reply

18 sara August 21, 2010

in fedora 12 i switch to root by command : su
and ./install.sh but the error come out: install.sh: No such file or directory
why?
i want to know how it detect the folder install.sh is inside?

Reply

19 sara August 21, 2010

switch to root with command: su -

Reply

20 MikeM November 2, 2010

So, I have a similar problem as Sara. Created a miketest.sh script, and used “./” prefix to execute it, and received “ksh: ./miketest.sh: not found.”

If I use “sh” instead of “./” it seems to run fine.
Permissions of miketest.sh are set to 777.
I have other scripts that run fine, and some that do not… What am I missing here?

Reply

21 pari February 28, 2011

may be u r not giving the correct path, check ur path again. Firstly reach to ur folder n then switch to root. I know it is very basic thing but sometimes mistake happens in such things also…

Reply

22 nathan June 13, 2011

nice one for beginners.. good working

Reply

23 shin_chan July 6, 2011

Alright a very nice tutorial….really helpful but I have a little problem…..
I want to run the .sh in background so that even I can logout.
I am using the command “nohup sh filename.sh &” and a file named “nohup.out” is also made but it contains an error which says “no such file or directory”…I am using the command in folder where the .sh file is located.
Pls tell where I am doing it wrong!!!!

Reply

24 Clifford February 3, 2012

Put the following at the end of the syntax:

2>&1 &

I hope this helps!!!

Reply

25 sonaht August 23, 2011

Thanks for this good and clear information. Very helpful for eachothers.. that are beginners :)

Reply

26 Areeg September 18, 2011

in ubunto 11.04
you need to specify the full path of the file not only th name .sh

Reply

27 Dhruv September 22, 2011

very nice … i need some more article on that particular topics can you please send me some link so that i can check daily

Reply

28 kishore November 7, 2011

i m getting the error output ……The program returned an error code (126)……..
when i run the sh script.sh

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 12 + 2 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: