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.
I think your script should work. But you might add something to it to get a little more information:
when running this script (named my_open.sh) in a terminal like this:
I get the following output:
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:
I get the following output:
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...).
Make a bash script test.sh as:
Then, make the script executable as:
Finally, run the script as:
Maybe gnome-open instead of xdg-open
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.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
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
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'
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
Close out gedit & in the terminal
Restart to add ~/bin to your $PATH
To invoke open a terminal and go
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"