æ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:~$
xnee
now.