How to start a terminal with certain text already input on the command-line?
Solution 1
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
interact
exit
and run Gnome Terminal with:
gnome-terminal -e ~/bin/myprompt
Solution 2
æ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.
#!/usr/bin/expect
#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)
} WINCH
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
interact
exit
and run Gnome Terminal with:
gnome-terminal -e "~/bin/myprompt \"my text to be posted\""
Solution 3
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:
history -s "$BASH_INITIAL_COMMAND"
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 #
.
Solution 4
æ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
#!/bin/sh
# terminal-plus-command: start a subordinate terminal which runs
# the interactive shell after first running the command arguments
tmpscript=/tmp/tmpscript.$$
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
script.sh '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 rt.sh
then the chmod
makes it executable and then it is executed.
$ chmod +x rt.sh && ./rt.sh 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:
msw@myhost:~$
Related videos on Youtube
Comments
-
emf over 1 year
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?
-
Michael Gundlach over 13 years+1 for using Expect. I've used this for years to help automate a lot of things.
-
RusGraf over 13 yearsI 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. -
emf over 13 yearsThis 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.
-
emf over 13 yearsThis looks cool. Can you explain what is happening here?
-
emf over 13 yearsThough not quite the solution I was looking for, this is interesting in its own right.
-
emf over 13 yearsclose, 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.
-
emf over 13 yearsIf there was a solution to this, it would solve most of the cases I'm looking for.
-
emf over 13 yearsThis 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?
-
RusGraf over 13 yearsIf all you want is the terminal to remain open after a command, there are much simpler solutions.
-
msw over 13 yearse.m.fields: I annotated it for you.
-
emf over 13 yearsyou rock. thanks for the annotations, this kind of help makes barrier-to-entry with scripting much more accessible
-
bioShark over 12 yearsSuper, 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.
-
Glutanimate almost 10 yearsBeautiful solution! I've added this as an external tool to gedit. Now it's much easier to quickly test my scripts while working on them (I prefer an external terminal over the one built into gedit).