Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Rather than rephrasing my question, let me describe to you the desired user-case:

I create a short shell-script to run command "gnome-terminal --someoptionflagname 'my text to be posted'", and execute this script.

Gnome-terminal pops up, with command-line prompt followed by my text.

ie: fields@mycomputer:/$ my text to be posted

Can this be done?

share|improve this question
If this bug is ever fixed in Ubuntu you should look into cnee. – ændrük Oct 8 '10 at 19:46

6 Answers 6

up vote 24 down vote accepted

You can do this with expect (install). Create and make executable ~/bin/myprompt:

#!/usr/bin/expect -f

# Get a Bash shell
spawn -noecho bash

# Wait for a prompt
expect "$ "

# Type something
send "my text to be posted"

# Hand over control to the user


and run Gnome Terminal with:

gnome-terminal -e ~/bin/myprompt
share|improve this answer
+1 for using Expect. I've used this for years to help automate a lot of things. – Marco Ceppi Oct 9 '10 at 0:42
This is cool. I was hoping there would be some simple(ish) answer based on default gnome-terminal functionality, but looks like this answers the job. – e.m.fields Oct 10 '10 at 2:38
This one has been bugging me so long, I almost couldn't remember what the intended use-case was. So, for clarification: I would want to do something like "gnome-terminal -e ls", but the catch is to have the window remain open. So, basically shortcuts to opening a terminal and running shell commands. Does this clarify issues vs aendruk/msw's answers? – e.m.fields Oct 10 '10 at 2:47
If all you want is the terminal to remain open after a command, there are much simpler solutions. – ændrük Oct 10 '10 at 3:21
Super, thanks. I've successfully integrated this into a shell script that call calls Pentaho data integration tool. Then I've added the call to this script to Unity launcher in 11.10, and it works like a charm. – bioShark Dec 2 '11 at 2:17

ændrük's suggestion is very good and worked for me, however the command is hardcoded within the script, and if you resize the terminal window it doesn't work well. Using his code as the base, I've added the ability to send the myprompt script the command as an argument, and this script correctly handles resizing the terminal window.


#trap sigwinch and pass it to the child we spawned
#this allows the gnome-terminal window to be resized
trap {
 set rows [stty rows]
 set cols [stty columns]
 stty rows $rows columns $cols < $spawn_out(slave,name)

set arg1 [lindex $argv 0]

# Get a Bash shell
spawn -noecho bash

# Wait for a prompt
expect "$ "

# Type something
send $arg1

# Hand over control to the user


and run Gnome Terminal with:

gnome-terminal -e "~/bin/myprompt \"my text to be posted\""
share|improve this answer

If I understand correctly, you want your first input line to be prefilled to contents that you pass on the gnome-terminal command line.

I don't know how to do exactly this with bash, but here's something that comes close. In your ~/.bashrc, add the following line at the very end:


Run gnome-terminal -x env BASH_INITIAL_COMMAND='my text to be posted' bash, and press Up at the prompt to recall the text.

Note also that if you put set -o history followed by comments at the end of your .bashrc, they will be entered into the history as bash starts, so you can use them as a basis for editing by reaching them with the Up key and removing the initial #.

share|improve this answer
Though not quite the solution I was looking for, this is interesting in its own right. – e.m.fields Oct 10 '10 at 2:39

ændrük's answer is fine, but perhaps a little heavyweight for the task.

Here is a script that writes a script based on its arguments

# terminal-plus-command: start a subordinate terminal which runs
# the interactive shell after first running the command arguments

echo "#!$SHELL" > $tmpscript
echo "$@" >> $tmpscript
echo exec "$SHELL" >> $tmpscript
chmod +x $tmpscript
gnome-terminal --command $tmpscript
rm -f $tmpscript

If you've not done much shell programming, there appears to be more magic here than there is. First, I name a temporary file for holding the script where $$ is the process ID of the shell running this script. The /tmp/something.$$ metaphor is used in case two instances of this script are run at the same time, they won't try to use the same temporary file.

The variable $SHELL is set to the name of the shell running the script. If you use /usr/bin/bash, presumably you'd like the mini-script to use it also.

The "$@" is a shell idiom for "interpolate all my arguments, quoting them if needed". This peculiar syntax causes 'my file' your\ file

to interpolate the arguments as two elements

"my file" "your file"

instead of the four that $@ would yield

"my" "file" "your" "file"

The last lines of the script arrange for a gnome-terminal to start running the mini-script and then starting an interactive shell. When the gnome-terminal exits, the temporary script is removed because littering is uncool.

The last line is not a part of the mini-script, it demonstrates that the mini-script works. If the 11 line script above is in a file called then the chmod makes it executable and then it is executed.

$ chmod +x && ./ echo hello world

The result of all of this will be a gnome terminal which starts up, displays

hello world

on its first line and then starts an interactive shell:

share|improve this answer
I think your script could be reduced to gnome-terminal -x sh -c "echo hello world; bash", which I don't think the question is asking for. – ændrük Oct 9 '10 at 22:13
This looks cool. Can you explain what is happening here? – e.m.fields Oct 10 '10 at 2:38
e.m.fields: I annotated it for you. – msw Oct 10 '10 at 4:35
you rock. thanks for the annotations, this kind of help makes barrier-to-entry with scripting much more accessible – e.m.fields Oct 11 '10 at 4:41

The two answers I've liked best to solve this are using expect and this one where they recommend using the --init-file flag either in the shebang or when executing the terminal:

#!/bin/bash --init-file
commands to run

... and execute it as:

xterm -e /path/to/script
# or
gnome-terminal -e /path/to/script
# or
the-terminal -e bash --init-file /path/to/script/with/no/shebang

expect is probably the better solution (for reasons I'll outline), except that I cannot control whether it's installed in my target environment.

The problem I ran into with bash's --init-file and gnome-terminal's --command as a hack to solve this is the shell doesn't have access to STDIN while executing init scripts. If, for example, I want to run something that would require user input (ftp, telnet, etc) but leave the parent shell running afterward it won't work; the only exception I've seen to this so far is ssh:

xterm -e /bin/bash --init-file <(echo 'ssh -X some-machine')

... but what I need is more complicated than this toy example. Anyway, I hope the --init-file stuff is useful to anyone reading this question.

share|improve this answer

You can use the -e or -x arguments to run a command inside a newly popped up terminal, but that's not exactly what you want.

share|improve this answer
close, but not quite what I'm looking for. One problem, for this case, is that when executed and finished, the terminal will immediately close. So, let's say I want a shortcut to "ls ~/notes", it will close before I can view the output. – e.m.fields Oct 10 '10 at 2:41
If there was a solution to this, it would solve most of the cases I'm looking for. – e.m.fields Oct 10 '10 at 2:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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