Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

Trying to create a bash script that opens gnome-terminal, and then runs ls to display the contents of a directory, but it just opens gnome-terminal. I will be creating some other scripts that also use multiple commands.

My script:

#!/bin/bash
gnome-terminal
ls -a /examplefolder

EDIT:

To clarify what I'm trying to achieve. I'm trying to create a script that will open gnome-terminal, list the contents of /examplefolder, and then be ready for regular terminal usage. Is this possible, or am I barking up the wrong tree?

share|improve this question

marked as duplicate by Radu Rădeanu, karel, Sylvain Pineau, Eric Carvalho, mniess Jun 3 at 19:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers 4

(For Ubuntu 12.04) The problem here is that with your script bash will open a gnome-terminal and then executed the command in you current terminal instead of the newly opened one (if that's what you are actually trying to do.)

Note: Also as suggested by @unorthodox grammar & should be used at the end of the command that you don't want to wait for (allowing them to run in background). However, interestingly that's not the problem at least in Ubuntu 12.04. But perhaps in other Ubuntu flavors this might be an issue.

You can execute the commands in the newly opened gnome-terminal by using the following command:

gnome-terminal -e "ls -a /examplefolder"

or

gnome-terminal --command "ls -a /examplefolder"

However, this would close the terminal as soon as the commands are executed and if you want to see the result of those command you won't be able to. Note that gnome-terminal is not designed for this use case (seeing the results in a newly opened gnome-terminal from script). One of the work around as defined in this nice answer is the following:

Let gnome-terminal run bash and tell bash to run your commands and then run bash

gnome-terminal -e "bash -c \"echo foo; echo bar; exec bash\""

or

gnome-terminal -x bash -c "echo foo; echo bar; bash"

The exec bash at the end is necessary because bash -c will terminate once the commands are done. exec causes the running process to be replaced by the new process, otherwise you will have two bash processes running.

You can refer to this answer for further workarounds.

share|improve this answer
1  
Or gnome-terminal -x bash -c "echo foo; echo bar; bash" –  TuKsn Jun 2 at 8:53
    
Yes. Thanks. Edited! +1 –  MB_CE Jun 3 at 7:20

In a bash script, commands are run one after another. Your script launches gnome-terminal, waits for it to terminate then runs ls -a /examplefolder. To run a command through gnome-terminal, use the -x parameter:

gnome-terminal -x bash -c 'ls -a /examplefolder; read -p "Press [ENTER] to continue..."'

This will start bash in the newly opened terminal, and make it run the ls and read commands.

By the way, do you have any specific reasons to start a to start a terminal from a shell script? (Usually it's the other way around.) If you just want to start a shell script from your desktop and see it's results in a terminal, you could just create a launcher for the script and select "run in terminal", at least in Xubuntu, but I'm pretty sure other flavours have a similar option.

share|improve this answer
    
This should work but the answer would be better if you explained why the OP's code doesn't work and why this does. –  Oli Jun 1 at 22:48
    
Thanks for the suggestion, edited my answer. –  kraxor Jun 1 at 23:01
1  
@kraxor is there anyway to keep the terminal open afterwards? I'm using this script like a shortcut, it opens a terminal, runs this command, and then is ready for use. I don't want to use a .desktop file unless I have no choice. –  please delete me Jun 1 at 23:20

Show output in terminal

gnome-terminal -x  bash -c "echo foo; echo  bar; read || sleep 9999"  

or in GUI widow

 ( echo foo;  sleep 5; echo  bar ) | zenity --text-info
share|improve this answer

For commands to execute 1 after another without waiting for previous to finish:

#!/bin/bash
ls -a /examplefolder &
leafpad &
ls -a /home &
exec bash

For commands to execute 1 after another, waiting for previous to finish first:

#!/bin/bash
ls -a /examplefolder
leafpad
ls -a /home
exec bash

As suggested by MB_CE's answer the use of "exec bash" as a final command can be used to hold the terminal open, because technically bash is still running.

share|improve this answer

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