529

Whenever I open a .sh file, it opens it in gedit instead of the terminal. I can't find any option similar to Right ClickOpen WithOther Application...Terminal.

How do I open this file in the terminal?

pomsky
  • 70,557
Alex
  • 5,291

17 Answers17

791

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
Jeremy Kerr
  • 27,829
karthick87
  • 84,513
112

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

Isaiah
  • 60,750
80

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

sh <name of file>.sh
edwin
  • 3,829
33

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
pomsky
  • 70,557
25

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.

kos
  • 41,268
12

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

11

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

Zanna
  • 72,312
7

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.

wjandrea
  • 14,504
Kenny Stier
  • 185
  • 1
  • 7
6

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 ./

Joren
  • 5,074
rob
  • 61
  • 1
  • 1
5
  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.

David Foerster
  • 36,890
  • 56
  • 97
  • 151
4

In Ubuntu 16.04 this is how to open it in Terminal:

Go to the File Manager > Edit > Preferences > Behavior for Executable Text Files and select Ask each time.

The problem is that it's by default set to View Executable Files when they are opened.

3

If you place your shell script or other executable you create in /usr/local/bin it will be found and executed without having to provide a folder path in the command line or adding ./ to the name. For example I created the following simple 3 line bash script to display disk UUIDs:

#!/bin/bash
echo "* UUIDs must match in /etc/fstab and /boot/grub/menu.lst"
sudo blkid

I called the file uuid and placed it in /usr/local/bin. All I need enter on the command line is:

uuid
fragos
  • 3,583
2

The problem I have found on a few distributions is they have hidden the preferences option in Nautilus, but to fix it in Ubuntu and other distributions using Gnome3 is the same (literally just done the Fedora version of this and posting the actual fix to remind me how in the future).

  1. Install dconf-editor

    sudo apt-get install dconf-editor
    
  2. Run dconf-editor using the user account you want this on, i.e NOT root

    dconf-editor
    
  3. Navigate to the following schema:

    org.gnome.nautilus.preferences

  4. Change the default option to not open by default:

    Find executable-text-activation click the word display and change to ask

that will give you the option to edit, view or run the file going forward

wjandrea
  • 14,504
Snider
  • 21
2

Well, I too faced the same problem. I wanted to execute the .sh file and it opened with Gedit on CentOS 7. So here is what I did:

  1. I navigated to the path of the .sh file I wanted to execute.
  2. I opened the terminal.
  3. And I simply dragged and dropped the on the terminal window and it automatically took that file along with the path as input.
  4. Hit Enter and you are good to go!
grooveplex
  • 2,506
  • 3
  • 27
  • 35
2

I am a noob in Linux and I just had the same problem. If all else fails:

  1. Open terminal
  2. Open the folder containing the .sh file
  3. Drag and drop the file into the terminal window
  4. The file's path appears in terminal. Press Enter.
  5. Voila, your .sh file is run.
wjandrea
  • 14,504
0

Finally got it figured out. On Ubuntu 22, April 2024

So, besides checking Allow executing file as program In the Permissions tab of Properties you have to rightclick your shell(sh) file and press Run as a program EACH TIME you gonna launch it !!!

Otherwise it will get opened by Text Editor or by whatever you have assigned it to be opened in third tab, namely Open with, of Properties popup before.

-3

You can also use . tricks, with the suggestion of other answers.

For example:

chmod +x filename.sh, assuming you have already gone to the path to file then run the file with any one of these command:

sh filename.sh

./filename.sh

. filename.sh
Awais
  • 305
  • 1
  • 3
  • 9