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?
You can do this with expect (install). Create and make executable
~/bin/myprompt
:and run Gnome Terminal with:
æ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.
and run Gnome Terminal with:
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#
.æ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
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 causesto interpolate the arguments as two elements
instead of the four that
$@
would yieldThe 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 thechmod
makes it executable and then it is executed.The result of all of this will be a gnome terminal which starts up, displays
on its first line and then starts an interactive shell:
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:... and execute it as:
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
andgnome-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:... 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.To start in interactive bash inside a gnome-terminal with some commands already executed I put those commands in a setup file and dynamically append the contents to my
.bashrc
file: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.