2

I'm fairly good with windows .bat scripts and normally use the pause command while developing them so I can see things step by step as the script runs in the command prompt. I am trying to do something similar with a Linux Bash file. I have a very simple bash, which can be seen below...

#!/bin/bash
echo Hello World!
read -p "Press [Enter] key to continue..."

I configured Nautilus (a Linux file management program) to "Run executables when they are double clicked" but I don't see anything when I double click the script. Do I need to re-route output to the console or something?

Edit

Here is how I achieved what I wanted...

  1. Create a .sh file (I am using one from above)
  2. Open Nautilus
  3. Click Edit -> Preferences -> Behavior tab
  4. Under "Executable Text Files" choose "Ask Each Time" Radio button

Now when you click on .sh files, you will be prompted with a few options. Option "Run in Terminal" does exactly what I want. Thanks everyone for the contributions!

  • Look at your running processes with ps -ef, you will see something running there. – John WH Smith Sep 7 '14 at 19:26
2

You need to launch a terminal, which in turn launches the script.

Try this:

#!/bin/sh
xterm -e "echo Hello World; read -p 'Press [Enter] key to exit ..."

This is of course impractical for a longer script. But then you can simply do:

#!/bin/sh
xterm -e /home/user/scripts/thescript.sh

and thescript.sh contains all the commands you want to use. Make sure it is executable (chmod +x thescript.sh)

  • I figured out how to do exactly what I wanted, I added the edit above. What exactly does xterm do? – Jason Sep 8 '14 at 20:23
  • Congratulations. Xterm is just a terminal emulator. See man xterm. You might prefer another one. E.g. gnome-terminal. – Sebastian Sep 9 '14 at 4:49
1

Your script needs a terminal for you to interact with it. One way of doing that is what @Sebastian suggested. However, note that bash scripts are not really designed to be run this way. A cleaner way would be to write a .desktop file that launches your script and then double click that. Something like:

[Desktop Entry]
Exec=/home/user/yourscript.sh
Terminal=true
Type=Application

Save that file as foo.desktop in your ~/Desktop folder. That will now appear as an icon there and double clicking it will cause your script to be run in a terminal. Obviously, you need to change /home/user/yourscript.sh to the actual path of your script.

  • With your terminal emulator, how can we redirect stdin , stdout and stderr to /dev/null? Thank you. – Frank May 28 '16 at 4:50
  • @Frank I don't know. I would do it by editing the script itself or writing a wrapper script. – terdon Jun 1 '16 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.