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?
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?
Give execute permission to your script:
And to run your script:
Since
.
refers to the current directory: ifyourscript.sh
is in the current directory, you can simplify this to:You need to mark shell scripts as executable to run them from the file manager:
Right click on your
.sh
file and select Properties:In the Permissions tab, check Allow executing file as program:
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:
Open a terminal and navigate to the folder where the
.sh
file is located. Then type:Prerequisite
Before you can run the .sh file, you need to make it executable:
Warning
Make sure you trust the source where you got the file from. It could be a virus.
The very simple way
This has problem. The terminal will close immediately and you will not be able to see the output.
The simple way
The way professionals do it
Find where the .sh file
ls
andcd
commandsls
will list the files and folders in the current folder. Give it a try: type "ls" and press Enter.cd
, followed by a space, followed by a folder namecd ..
to go one level upRun the .sh file
Once you can see for example
script1.sh
withls
run this: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
andcd
. Once you are in the correct current folder you can run the script like this:or you can run and redirect the output to a file:
or you can filter the output for keywords (e.g. "apples") an then redirect to a file:
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:
And then open the file like this:
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.
Go to the directory where the
.sh
file is by usingcd
. In this example I have stored mysh
file as~/Desktop/shell_practice/test.sh
first do
pwd
to figure out where you are, and if it returns/home/username
(whereusername
is your real username), you can runIf you seem to be somewhere else, you can use the absolute path
or
or even
these are all ways of describing the same place. Once you've made it to the location of your script, type
If you can see the
sh
file in the output, you can usechmod
to make it executable. In my case, remember, the filename istest.sh
, so I would runNow 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:If your script has been written correctly it will run without errors...
Here's a live example:
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.
There are a few ways to do this.
Option 1
In the terminal, access the directory the Bash file is in using
cd
(change directory).Ex.
cd Downloads
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
In the terminal, navigate to the directory the bash file is in.
Run
chmod +x <filename>.sh
In Nautilus, open the file.
2 main steps.
in terminal, use gedit to write and save script with ".sh" extension to desktop. (but any text editor can be used)
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./
Right-click the
.sh
file and make it executable.Open a terminal (Ctrl+Alt+T).
Drag the
.sh
file into the terminal window and watch in awe.