What is an example of a generic shell script that will run correctly in the terminal but not when double-clicked?
Sophia Terry
I'd really like to understand the underlying reasons why some scripts work from the command line but not when double-clicked.
Is there an example of a script that does something simple, like echoing a variable or something like that, which would reproducibly work in the command line, but then fail to work when double-clicked?
To be clear, I mean it should run in both instances, but when double-clicked it should fail to perform its task correctly, and it should do this on any recent Ubuntu installation. I'm hoping that by being able to reproduce the behaviour I can understand it and fix it myself in the future.
12 Answers
Easiest example:
create a file "test" and put
#!/bin/bash echo "Hello World!"in it
- make it executable with
chmod 700 test do
./teston commandline and it will outputHello world!
From Nautilus:
- default: doubleclick and it will open as a text file
- when set to "run text files" from Nautilus preferences it will flicker a couple of times and then does nothing.
But this is intended behaviour. For Nautilus you would need to create a script that does a "pop up" with the text "Hello world!" in it.
Example:
#!/usr/bin/python
import os
os.system('zenity --info --text="Hello world!!"')would show ...
By the way: this also works from command line when you have a desktop. On a tty it would show an error "Failed to connect to Mir".
and fix it myself in the future
I doubt though there is something to fix. Executing in command line and executing in Nautilus are 2 different things.
2Ok I think I've got the answer... at least this solves my problem and along the way I stumbled on this and thought I would come back and post an answer.
Shell scripts do not have the same $PATH variable that the terminal does. I can change that for the purpose of the script (in my case using Jupyter notebook as an example) by doing this:
#! /usr/bin/env sh
#this adds the location where jupyter command is found
export PATH="/home/username/anaconda3/bin:$PATH"
jupyter notebookand that will work.
Additionally some programs may need the terminal to interact with so you could do something like:
#! /usr/bin/env sh
#this adds the location where jupyter command is found
export PATH="/home/username/anaconda3/bin:$PATH"
xterm -e "jupyter notebook" # brings up a terminal windowAnd that will bring up a terminal window to display output and receive input.
Edit: I changed for completeness.