SnapOverflow

SnapOverflow Logo SnapOverflow Logo

SnapOverflow Navigation

  • Home
  • Server
  • Ubuntu

Mobile menu

Close
  • Home
  • System Administrators
    • Hot Questions
    • New Questions
    • Tags
  • Ubuntu
    • Hot Questions
    • New Questions
    • Tags
  • Help
Home / ubuntu / Questions / 38661
In Process
Alex
Alex
Asked: 2011-05-01 18:39:09 +0800 CST2011-05-01 18:39:09 +0800 CST 2011-05-01 18:39:09 +0800 CST

How do I run .sh scripts?

  • 772

Whenever I open a .sh file, it opens it in gedit instead of the terminal. I can't find any option similar to Right Click → Open With → Other Application... → Terminal.

How do I open this file in the terminal?

command-line
  • 16 16 Answers
  • 3794271 Views

16 Answers

  • Voted
  1. karthick87
    2011-05-01T19:18:21+08:002011-05-01T19:18:21+08:00

    Give execute permission to your script:

    chmod +x /path/to/yourscript.sh
    

    And to run your script:

    /path/to/yourscript.sh
    

    Since . refers to the current directory: if yourscript.sh is in the current directory, you can simplify this to:

    ./yourscript.sh
    
    • 702
  2. Isaiah
    2011-05-01T19:02:29+08:002011-05-01T19:02:29+08:00

    You need to mark shell scripts as executable to run them from the file manager:

    1. Right click on your .sh file and select Properties:

      enter image description here

    2. In the Permissions tab, check Allow executing file as program:

      enter image description here

    3. Close the Properties window and double-click the file. A dialog will pop up giving you the option to run the script in a terminal:

      enter image description here

    • 109
  3. italianfoot
    2012-12-20T23:43:11+08:002012-12-20T23:43:11+08:00

    Open a terminal and navigate to the folder where the .sh file is located. Then type:

    sh <name of file>.sh
    
    • 64
  4. Aleksandr Levchuk
    2011-05-01T18:52:25+08:002011-05-01T18:52:25+08:00

    Prerequisite

    Before you can run the .sh file, you need to make it executable:

    1. Right-click on the file
    2. Select Properties
    3. Select Permissions
    4. Select Allow executing file as a program

    Warning

    Make sure you trust the source where you got the file from. It could be a virus.

    The very simple way

    1. Double-click on the file
    2. Click run in terminal

    This has problem. The terminal will close immediately and you will not be able to see the output.

    The simple way

    1. Open Applications -> Accessories -> Terminal
    2. Drag and drop the .sh file into the terminal and press Enter

    The way professionals do it

    1. Open Applications -> Accessories -> Terminal
    2. Find where the .sh file

      • Use the ls and cd commands
      • ls will list the files and folders in the current folder. Give it a try: type "ls" and press Enter.
      • Once you see the folder that you want to go in to, run cd, followed by a space, followed by a folder name
      • If you when into a folder that you did not want, run cd .. to go one level up
    3. Run the .sh file

      • Once you can see for example script1.sh with ls run this:

        ./script.sh
        

    Why do it the complicated way?

    The terminal has a rich set of powerful tools that are accessible by typing the commands. Professionals locate the .sh file by typing ls and cd. Once you are in the correct current folder you can run the script like this:

    ./script1.sh
    

    or you can run and redirect the output to a file:

    ./script1.sh > out.txt
    

    or you can filter the output for keywords (e.g. "apples") an then redirect to a file:

    ./script1.sh | grep apples > ./only-apples
    

    There are thousands of things you can to to that file just by typing a few commands.

    Another one, you can download a file from the Internet with one simple command:

    wget www.google.com/images/logos/ps_logo2.png
    

    And then open the file like this:

    shotwell ps_logo2.png
    
    • 33
  5. imjustmatthew
    2013-06-11T13:41:59+08:002013-06-11T13:41:59+08:00

    On Ubuntu 13.04 executable files opened in Nautilus are now opened in gedit by default rather than prompting the user to execute them. To enable the classic behavior you need to adjust the preferences:

    Nautilus → Edit menu → Preferences → Behaviour tab → Click the radio button near Ask each time.

    • 23
  6. TheExorcist
    2016-12-04T03:14:20+08:002016-12-04T03:14:20+08:00

    Go to the directory where the .sh file is by using cd. In this example I have stored my sh file as ~/Desktop/shell_practice/test.sh

    first do pwd to figure out where you are, and if it returns /home/username (where username is your real username), you can run

    cd Desktop/shell/practice
    

    If you seem to be somewhere else, you can use the absolute path

    cd ~/Desktop/shell/practice
    

    or

    cd $HOME/Desktop/shell/practice
    

    or even

    cd /home/$USER/Desktop/shell/practice
    

    these are all ways of describing the same place. Once you've made it to the location of your script, type

    ls
    

    If you can see the sh file in the output, you can use chmod to make it executable. In my case, remember, the filename is test.sh, so I would run

    chmod u+x test.sh
    

    Now that we are in the same directory as the script, we have to specify to the shell that we want to execute the file by giving its location ./ (the current directory followed by a path separator, to distinguish it from the filename). To run my file I would type:

    ./test.sh
    

    If your script has been written correctly it will run without errors...

    Here's a live example: Here is live example

    • 10
  7. Akhilesh Dhar Dubey
    2018-08-17T02:18:13+08:002018-08-17T02:18:13+08:00

    For Ubuntu 18.04, There is a little modification, as you don't get a pop-up dialog.

    So what you need to do is:

    Right click on Files, Select Preferences > Select Behavior Tab > Mark 'Ask what to do' option under Executable text file.

    Now, When you double-click on any .sh file, you will get a popup, there you can select "run in terminal" option to run your .sh file.

    enter image description here

    • 10
  8. Kenny Stier
    2014-01-07T13:33:04+08:002014-01-07T13:33:04+08:00

    There are a few ways to do this.

    Option 1

    1. In the terminal, access the directory the Bash file is in using cd (change directory).

      Ex. cd Downloads

    2. Run bash <filename>.sh

      This also works with .run files. There is an example of this usage at this webpage on updating Rhythmbox.

    Option 2

    1. In the terminal, navigate to the directory the bash file is in.

    2. Run chmod +x <filename>.sh

    3. In Nautilus, open the file.

    • 7
  9. rob
    2013-10-25T06:24:49+08:002013-10-25T06:24:49+08:00

    2 main steps.

    1. in terminal, use gedit to write and save script with ".sh" extension to desktop. (but any text editor can be used)

    2. open Nautilus and right click the script.sh file.

      • under properties, check box "allow executing file.."

      • in Nautilus menu, click file,then preferences,then behaviour

      • check the "run executable text files when they are opened".

    Now, when you double click the file on the desktop, it should execute. no need for . or ./

    • 6
  10. 1p_bunny
    2017-06-11T06:09:43+08:002017-06-11T06:09:43+08:00
    1. Right-click the .sh file and make it executable.

    2. Open a terminal (Ctrl+Alt+T).

    3. Drag the .sh file into the terminal window and watch in awe.

    • 5

Sidebar

Stats

  • Questions 681965
  • Answers 980273
  • Best Answers 280204
  • Users 287326
  • Popular
  • Answers
  • Marko Smith

    How to unzip a zip file from the Terminal?

    • 9 Answers
  • Marko Smith

    How can I copy the contents of a folder to another folder in a different directory using terminal?

    • 8 Answers
  • Marko Smith

    How do I install a .deb file via the command line?

    • 11 Answers
  • Marko Smith

    How do I run .sh scripts?

    • 16 Answers
  • Marko Smith

    How do I install a .tar.gz (or .tar.bz2) file?

    • 14 Answers
  • Marko Smith

    What command do I need to unzip/extract a .tar.gz file?

    • 8 Answers
  • Marko Smith

    How to list all installed packages

    • 24 Answers
  • Marko Smith

    Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?

    • 25 Answers
  • Marko Smith

    How can I add a user as a new sudoer using the command line?

    • 7 Answers
  • Marko Smith

    Change folder permissions and ownership

    • 9 Answers
  • Martin Hope
    ubuntu-nerd How to unzip a zip file from the Terminal? 2011-12-11 20:37:54 +0800 CST
  • Martin Hope
    pandisvezia How can I copy the contents of a folder to another folder in a different directory using terminal? 2011-12-11 17:19:37 +0800 CST
  • Martin Hope
    Scott Szretter Where is the cron / crontab log? 2011-08-12 04:06:43 +0800 CST
  • Martin Hope
    TheXed How do I install a .deb file via the command line? 2011-05-07 09:40:28 +0800 CST
  • Martin Hope
    EmmyS What command do I need to unzip/extract a .tar.gz file? 2011-02-09 14:50:41 +0800 CST
  • Martin Hope
    Ivan How to list all installed packages 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra Unable to lock the administration directory (/var/lib/dpkg/) is another process using it? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry How do I determine the total size of a directory (folder) from the command line? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher "The following packages have been kept back:" Why and how do I solve it? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford How can PPAs be removed? 2010-07-30 01:09:42 +0800 CST

Related Questions

Trending Tags

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • Home
  • Questions
    • Hot Questions
    • New Questions
  • Tags
  • Help

Footer

SnapOverflow

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Help

© 2022 SOF-TR. All Rights Reserve