- Open a new terminal bash command in linux
- What is the command structure to open a new terminal and execute the given set on commands on this new terminal?
- Starting a Command in a New Terminal
- Run command on another(new) terminal window
- Programmaticaly open new terminal with Bash and run commands, keeping job-control
- Method one
Open a new terminal bash command in linux
This is as opposed to the «pre-quoting» method where the Here Document’s delimiter is deliberately non-quoted the Here String (the stdin redirection piece) must be the single-quoted type as well 2 , or the piece inside it would be expanded by the inner at a time when its array is not yet populated specifically, we need such stdin redirection (the one made via the Here String) as a helper just to make the inner «defer» the execution of the commands needing a fully populated array inside such helper stdin redirection (the Here String piece) we need to make the inner redirect its own stdin again, this time back to its controlling terminal (hence the line) to make it recover its interactive functionality we need all commands meant to be executed after the (ie the here) to be specified on the same line 3 as the because after such redirection the Here String will no longer be read 4 . Anyway, using simply as above would not work in case of filenames containing spaces or newlines because at the time the inner consumes the sequence of commands those filenames are not quoted and hence the spaces in filenames are interpreted as word separators as per standard behavior.
What is the command structure to open a new terminal and execute the given set on commands on this new terminal?
x-terminal-emulator doesn’t start a shell by itself. This leafs just executables to be started with the -e option.
While echo is available as an executable ( /bin/echo ), read as a bash internal command will fail without bash . Therefore the output in the new window is done faster than it takes to open the window and as read fails, the window is closed before you see it.
x-terminal-emulator -e "bash -c 'echo $PATH; read'"
Now x-terminal-emulator starts a bash shell which then will execute echo $PATH; read . As echo and specially read now are available as bash internal commands, the read command will not fail and wait for an input, which keeps the window open until a key is pressed.
How to Launch a Terminal Window on Ubuntu Linux, To quickly open a Terminal window at any time, press Ctrl+Alt+T. A graphical GNOME Terminal window will pop right up. A Terminal window on a
Starting a Command in a New Terminal
http://filmsbykris.com/wordpress/?p=1270 Chat with us and learn more http://FilmsByKris.com Duration: 7:39
Run command on another(new) terminal window
This might be what you run:
gnome-terminal -- sh -c "bash -c \"!!; exec bash\""
In older versions, -e and -x were used:
gnome-terminal -e "bash -c \"!!; exec bash\"" # or gnome-terminal -x sh -c "!!; bash"
It opens gnome-terminal with your last command ( !! ) executed and it stays open with the command output in the shell, even with an interactive command like top or less .
gnome-terminal -- sh -c "bash -c \"apropos editor; exec bash\""
gnome-terminal -c "bash -c \"apropos editor; exec bash\"" # or gnome-terminal -x sh -c "apropos editor; bash"
Start another instance of whatever terminal is it you want to run:
xterm -hold -e 'apropos editor' &
Note the -hold . Most terminals will exit after running the command you feed them. There are already a dozen or so questions about this on the site:
- How can I make a script that opens terminal windows and executes commands in them?
- How to run a script without closing the terminal?
An alternative to that is to use an application which needs to be exited. nano will stay open on its own. If you’re just outputting to screen, you could pipe it into less :
xterm -e 'apropos editor | less' &
That said, in your case (as the other two have said) it does seem easier that you just open another terminal and run your command.
Each terminal is even a program that you can launch as any other program, with & to put in background, giving a list of arguments and so on.
Which terminal to use it depends first from the availability of the system that you are using (if they are installed or not), after from their peculiarity and then from your personal taste.
konsole --hold -e "ls" & xterm -hold -e "ls" & gnome-terminal -e "ls" & .
Note the differences between -hold of xterm and —hold of konsole .
Each realization has different options that you have to check with the help. Even the help can be invoked in different way. You can find that man konsole doesn’t function and so you have to ask directly to the executable with —help .
This is a list of terminal you can search on your system
aterm - AfterStep terminal with transparency support gnome-terminal - default terminal for GNOME guake - A dropdown terminal for GNOME konsole - default terminal for KDE Kuake - a dropdown terminal for KDE mrxvt - Multi-tabbed rxvt clone rxvt - for the X Window System (and, in the form of a Cygwin port, for Windows) rxvt-unicode - rxvt clone with unicode support xfce4-terminal - default terminal for Xfce desktop environment with dropdown support Terminator - is a GPL terminal emulator. It is available on Microsoft Windows, Mac OS X, Linux and other Unix X11 systems. Terminology - enhanced terminal supportive of multimedia and text manipulation for X11 and Linux framebuffer tilda - A drop down terminal wterm - It is a fork of rxvt, designed to be lightweight, but still full of features xterm - default terminal for the X Window System Yakuake - (Yet Another Kuake), a dropdown terminal for KDE
Starting a Command in a New Terminal, http://filmsbykris.com/wordpress/?p=1270 Chat with us and learn more http://FilmsByKris.com Duration: 7:39
Programmaticaly open new terminal with Bash and run commands, keeping job-control
I could think of a couple of methods, and I think the first one is less hackish on Bash mainly because it seems (to me) to have quirks that can be more easily handled. However, as that may also be a matter of taste, I’m covering both.
Method one
It consists in making your script expand its $@ array, thus doing it on behalf of the inner interactive bash , and you might use the /dev/fd/X file-descriptor-on-filesystem facility (if available on your system) as parameter to the —init-file argument. Such file-descriptor might refer to a Here String where you would handle the filenames, like this:
xterm -e bash --init-file /dev/fd/3 3
One bonus point of this file-descriptor-on-filesystem trick is that you have a self-contained solution, as it does not depend on external helper files such as your .vimbashrc . This is especially handy here because the contents of the --init-file is dynamic due to the $@ expansion.
On the other hand it has a possible caveat wrt the file-descriptor's actual persistence from the script all the way through to the inner shell. This trick works fine as long as no intermediate process closes the file-descriptors it received from its parent. This is common behavior among many standard tools but should e.g. a sudo be in the middle with its default behavior of closing all received file-descriptors then this trick would not work and we would need to resort to a temporary file or to your original .vimbashrc file.
Anyway, using simply $@ as above would not work in case of filenames containing spaces or newlines because at the time the inner bash consumes the sequence of commands those filenames are not quoted and hence the spaces in filenames are interpreted as word separators as per standard behavior.
To address that we can inject one level of quoting, and on Bash's versions 4.4 and above it is only a matter of using the @Q Parameter Transformation syntax onto the $@ array, like this:
xterm -e bash --init-file /dev/fd/3 3"
On Bash's versions below 4.4 we can obtain the same by using its printf %q , like this (as Here Document for better readability, but would do the same as a Here String like above):
printf -v files ' %q' "$@" xterm -e bash --init-file /dev/fd/3 3
On a side-note, depending on your setup you might consider sourcing /etc/bash.bashrc too, prior to the user's .bashrc , as that is Bash's standard behavior for interactive shells.
Note also that I left the set -m command for familiarity to your original script and because it's harmless, but it is actually superfluous here because --init-file implies an interactive bash which implies -m . It would instead be needed if, by taking your question's title literally, you'd wish a job-controlling shell yet not a fully interactive one. There are differences in behavior.
Method two
The Bash's (and POSIX) -s option allows you to specify arguments to an interactive shell just like you do to a non-interactive one 1 . Thus, by using this -s option, and still as a self-contained solution, it would be like:
xterm -e bash --init-file /dev/fd/3 -s "$@" 3
- the Here Document's delimiter specification must be within single quotes or the $@ piece inside the Here Document would be expanded by your script (without correct quoting etc.) instead of by the inner bash where it belongs. This is as opposed to the "pre-quoting" method where the Here Document's delimiter is deliberately non-quoted
- the Here String (the exec must be the single-quoted type as well 2 , or the "$@" piece inside it would be expanded by the inner bash at a time when its $@ array is not yet populated
- specifically, we need such stdin redirection (the one made via the exec
- inside such helper stdin redirection (the Here String piece) we need to make the inner bash redirect its own stdin again, this time back to its controlling terminal (hence the exec < /dev/tty line) to make it recover its interactive functionality
- we need all commands meant to be executed after the exec < /dev/tty (ie the vim "$@" here) to be specified on the same line 3 as the exec < /dev/tty because after such redirection the Here String will no longer be read 4 . In fact this specific piece looks better as a Here String, if it can be short enough like in this example
This method may be better used with an external helper file like your .vimbashrc (though dropping the self-contained convenience) because the contents of such file, with regard to the filename-arguments problem, can be entirely static. This way, your script invoked by the file-manager would become as simple as:
xterm -e bash --init-file .vimbashrc -s "$@"
and its companion .vimbashrc would be like:
. ~/.bashrc # (let's do away with the superfluous `set -m`) exec
The companion file still has the quirks but, besides any "cleanliness" consideration, one possible bonus point of this latter version (non self-contained) is that its whole xterm -e . command, away of the "$@" part, could be used directly by your file-manager in place of your "script", if it were so kind to allow you to specify a command which it dutifully splits on spaces to produce the canonical "argv" array along with the filenames arguments.
Note also that this whole -s method, in all its versions, by default updates the user's .bash_history with the commands specified inside the helper stdin redirection, which is why I've been keeping that specific piece as essential as possible. Naturally you can prevent such updating by using your preferred way, e.g. by adding unset HISTFILE in the --init-file .
Just as a comparison, using this method with dash would be far more convenient because dash does populate the $@ array for the script specified by the ENV environment variable, and thus a self-contained solution would be as simple as:
xterm -e env ENV=/dev/fd/3 dash -s "$@" 3
1 with the exception that $0 can't be specified, as opposed to when invoking a shell with -c option
2 if you used an additional Here Document it would need to have its delimiter quoted as well
3 actually on the same "complete_command" as defined by POSIX, meaning that you can still span multiple lines as long as they are part of the same "complete_command", e.g. when such lines have the line-continuation backslash or are within a compound block
4 this should be standard behavior, presumably deriving from the first, brief, paragraph of this sub-section
As a simple proof of concept, I tried:
arg=file-to-edit xterm -e bash --init-file vimbashrc
which seems to behave more or less as you specified, and allows you to pass arguments to your interactive command.
So, using a tempfile would probably result in something like
#!/bin/bash if [ "$1" = "" ] ; then . ~/.bashrc set -m (IFS=$'\n'; vi $(cat "$tmpfile")) else tmpfile=$(mktemp /tmp/vimstart.XXXXXX) for arg in "$@" ; do echo "$arg" >> $tmpfile done tmpfile=$tmpfile xterm -e bash --init-file ./vimstart rm "$tmpfile" fi
Because the filename may contain \n , you can also use the NULL character as separator:
#!/bin/bash if [ "$1" = "" ] ; then . ~/.bashrc set -m (IFS=$'\00'; vi $(cat "$tmpfile")) else tmpfile=$(mktemp /tmp/vimstart.XXXXXX) for arg in "$@" ; do echo -n "$arg" >> $tmpfile echo -n $'\x00' >> $tmpfile done tmpfile=$tmpfile xterm -e bash --init-file ./vimstart rm "$tmpfile" fi
How to open new terminal tabs in the same terminal window using, You can have several --tab options: gnome-terminal --tab -e bash --tab -e bash --tab -e cmd1 --tab -e cmd2. Tester with gnome-terminal 3.30.2.