23

Ok, so running gedit myfile.txt works well. But what about opening a file from inside a bash script, using the default desktop app linked to the filetype?

I've tried below, which works great when run manually in terminal, but when I put it in a bash file, nothing happens:

#!/bin/bash
xdg-open "myfile.txt"&

What should I do instead?

Please note that I need the file to stay open after the terminal is closed as well.

Seth
  • 57,282
  • 43
  • 144
  • 200
Industrial
  • 1,688
  • 7
  • 22
  • 29
  • 1
    try providing the path of the file.. and are you sure it should be with quotes? Try without quotes – abhishek Jan 16 '12 at 11:50
  • 7
    Are you sure the command does nothing? I just created a script with the contents you list, and it opens `gedit` with the file listed (or opens a new tab in an existing session). It returns immediately though, rather than blocking until the program exits. – James Henstridge Jan 16 '12 at 11:55
  • 3
    In addition, what errors do you get when running your script from a terminal? – Takkat Jan 16 '12 at 12:01
  • Yep. I use the full path to the file in my script but removed it here to clarify. No difference with or without quotes. I've tried `xtg-open` on `*.txt`, `*.html` and more - just wont work. – Industrial Jan 16 '12 at 12:19
  • 5
    @abhishek: There are no cases where quotes are harmful in such situations, and plenty of cases where omitting quotes can cause problems. It's a good practice to make a habit of always quoting all strings, even when unnecessary, unless you have a specific reason not to. – Scott Severance Jan 18 '12 at 05:07
  • What do you mean with "nothing happens"? How do you envoke the bash script? Is the file executable? Does myfile.txt exist in the same directory as the script? – htorque Jan 20 '12 at 12:47
  • I envoke the script by double clicking on it and hitting "Run in terminal". Nothing seems to happen. File is executable. – Industrial Jan 20 '12 at 12:54
  • in a terminal, `cd` to the folder containing your script and invoke your script via `./scriptname` - any errors in the terminal output? – fossfreedom Jan 20 '12 at 12:56
  • Nope - that works great and `xdg-open` runs as intended, opening the file using the default editor. – Industrial Jan 20 '12 at 12:57
  • can you pastebin.com your full script file so that we can have a look if something else is causing the issue? – fossfreedom Jan 20 '12 at 13:09
  • Just to make clear - you want to have 1 script per 1 specific file or a generic one that opens any of the interested mimetypes with the set default application? If it's the former then based on how you're envoking you're using the wrong option, choose "run", there is no need for nor can you use "run in terminal" in that use case. If it's the latter then it can be done but has limited r. click on file value though can be interesting as a DnD default app launcher for ANY filetype – doug Jan 20 '12 at 15:36
  • One generic script that I want to use to open the default editor for the intended file. – Industrial Jan 21 '12 at 23:21
  • Make sure you are using `xdg-open` not `xtg-open` as stated above. – desgua Jan 25 '12 at 18:10
  • ```xdg-open "myfile.txt" &``` notice the space. –  Oct 11 '19 at 06:27

6 Answers6

17

I think your script should work. But you might add something to it to get a little more information:

#!/bin/bash
T=`xdg-mime query filetype $1`
echo "opening file "  $1  " of type " $T "with " `xdg-mime query default $T`
xdg-open $1
echo "finished script"

when running this script (named my_open.sh) in a terminal like this:

my_open.sh path/to/somefile.txt

I get the following output:

opening file  path/to/somefile.txt  of type  text/plain with  gedit.desktop
finished script

which tells me that the path to the file is ok, the mimetype is recognized and the desktopfile which is used to open the file is ok as well. And gedit opens with the file in question.

Now when run on another file:

my_open.sh path/to/README

I get the following output:

opening file  path/to/README  of type  text/x-readme with
finished script

Note the different mimetype and the missing desktop file. Nevertheless, xdg-open opens the default for all text files (gedit).

So, you might want to add something like this to your script and see if you get unexpected output (which you can then add to your question...).

xubuntix
  • 5,550
  • 23
  • 40
  • fine, except for the noisy `finished script` in the end. I recommend [http://www.catb.org/~esr/writings/taoup/html/](The art of Unix Programming), the rule of silence. Btw.: Backticks are deprecated - prefer $(...) instead, which is nestable. – user unknown Jan 21 '12 at 22:38
  • 2
    well, the script is meant to give additional information. The information given by 'finished script' is that you do not need `nohup` or `&`, but that `xdg-open` returns and lets you continue. – xubuntix Jan 22 '12 at 07:56
  • That's what the prompt is meant for. – user unknown Jan 22 '12 at 10:36
  • 3
    true. just thought it would be more obvious that way. actually, the whole script is not needed, because it does not more than `xdg-open` already does, so this is not a production quality script anyway... – xubuntix Jan 22 '12 at 10:43
5

Make a bash script test.sh as:

#!/bin/bash
gedit myfile.txt

Then, make the script executable as:

chmod +x test.sh

Finally, run the script as:

./test.sh
Uthman
  • 1,528
  • 1
  • 13
  • 13
  • Thanks for your answer, but unfortunately I need to open GNOME's default file editor for the file as mentioned above. – Industrial Jan 18 '12 at 15:35
3

Maybe gnome-open instead of xdg-open

Denis
  • 983
  • 5
  • 11
2

You are going in the correct direction. If you want the gui app to stay open when you close the terminal window then you just need to add a nohup at the start of the line.

#!/bin/bash
nohup xdg-open "myfile.txt"&

If the gui app is not opening its probably because you do not have the DISPLAY environment variable set in the shell you are trying to launch it from. Try doing an echo $DISPLAY

cweiske
  • 3,268
  • 2
  • 32
  • 42
Mark
  • 242
  • 1
  • 4
0

The first part of your question

with the command cat you can open a file inside the terminal, if that is what you want (it's stated in the first part of your question).

to use it you can just type cat FILENAME.

Other information

If you need more commands: Here's a good list of Commands.

GNOME default editor

If you want to open the file in GNOME's default application gedit.

to use it, just type gedit FILENAME

Alvar
  • 16,898
  • 29
  • 91
  • 134
  • That opens the file in the terminal, but not the default editor of GNOME – Industrial Jan 20 '12 at 12:55
  • @industrial edited my answer. – Alvar Jan 20 '12 at 13:14
  • Unfortunately, open wont work either... – Industrial Jan 20 '12 at 15:53
  • @Industrial What are you asking for? explain your question further! you say several different things and I have answered one of them but you say you don't want to use it... – Alvar Jan 21 '12 at 11:00
  • @Industrial the default text editor in GNOME **is** gedit aka "text editor" so if you run `gedit file.filetype` you will open the file as you want! – Alvar Jan 21 '12 at 11:01
  • Hi again! I appreciate your answer a lot. As mentioned in the question - I want to be able to "opening a file from inside a bash script, using the default desktop app linked to the filetype" - that's regardless of the file's being a `*.txt` or `.jpeg` or whatever. – Industrial Jan 21 '12 at 17:24
0

While I' not sure what's really to be accomplished here & based on some recent comments that a single script should open ANY file in default app or to extent the app supports this.

If so then easiest way to do that would be by opening a terminal & going scriptname /path/to/filename or if there are any spaces in path then scriptname '/path/to/filename'

cd; mkdir -p bin && gedit ~/bin/openit1

Use this as a script, you can use any name you wish for script, I'll use openit1 as example. It's best when using scripts directly from ~/bin to add a number to name so no conflicts with any existing linux commands

#!/bin/bash
xdg-open "$1"

Close out gedit & in the terminal

chmod u+x ~/bin/openit1

Restart to add ~/bin to your $PATH

To invoke open a terminal and go

openit1 /path/to/filename or openit1 'path/to/filename' 

IF as orig stated & using the orig. script for one particular file per script & invoking by d. left clicking on the script you just need to choose "Run" instead of "Run in Terminal"

doug
  • 16,848
  • 2
  • 45
  • 60