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...).
geditwith 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:55xtg-openon*.txt,*.htmland more - just wont work. – Industrial Jan 16 '12 at 12:19cdto the folder containing your script and invoke your script via./scriptname- any errors in the terminal output? – fossfreedom Jan 20 '12 at 12:56xdg-openruns as intended, opening the file using the default editor. – Industrial Jan 20 '12 at 12:57xdg-opennotxtg-openas stated above. – desgua Jan 25 '12 at 18:10xdg-open "myfile.txt" ¬ice the space. – Oct 11 '19 at 06:27