How to execute multiple commands after opening a new terminal tab - terminal

I need to open a new terminal tab and execute multiple commands on it, how can I do it. I have already tried the following,
gnome-terminal --tab -t "X" -e "cd ~/Desktop/terminal_test;mkdir test"
Here I need to cd into a new directory and create a new folder.

Try this:
gnome-terminal -x bash -c "cmd1; cmd2; …cmdN; exec bash"

You can use gnome-terminal. The script below will open 3 tabs running the respective commands..
tab="--tab"
cmd01="bash -c 'ls';'pwd';bash"
foo=""
foo+=($tab -e "$cmd01")
gnome-terminal "${foo[#]}"
exit 0

Related

Bash - open a new terminal tab then execute command

The following bash script is suppose to open 2 new terminal tabs then execute respective commands:
mate-terminal --tab -e "cd ~/ece344/root; sys161 -w kernel" --tab -e "cd ~/ece344/root; cs161-gdb kernel"
The script does open 2 new tabs however both tabs display the following error:
There was an error creating the child process for this terminal
Failed to execute child process "cd" (No such file or directory)
Ps. The answer should work with mate-terminal.
I don't have mate installed but I would try :
mate-terminal --tab -e "/bin/bash -c 'cd ~/ece344/root; sys161 -w kernel'" --tab -e "/bin/bash -c 'cd ~/ece344/root; cs161-gdb kernel'"
The idea is that "-e" would want to execute a command that probably run inside the window instead of a default shell, so from the error I understand that "cd" is not a real program in an expected location (since 'cd' is in the PATH shouldn't be a problem.
So my example would provide a full path to a shell "/bin/bash" that would then execute the commands you want.

How to start shell script with arguments in new terminal window

I tried to run shell script in new terminal window, from mac osx. I use open cmd like this and it works fine
open -a Terminal script.sh
My issue is I have to pass arguments in my shell. I tried
open -a Terminal script.sh arg
open -a Terminal script.sh --args arg
open -a Terminal --args script.sh arg
open -a Terminal "script.sh arg"
But nothing is working !!!
Do you know a way to do that?
I found something. I don't like this but it works:
osascript -e 'tell application "Terminal" to do script "cd ${Dir} && ./script.sh ${arg}"'
Have you tried sudo bash /path/to/script -option1 -option2(if your script needs sudo) or bash /path/to/script -option1 -option2(if no sudo is needed) from the terminal?

Bash: Execute command WITH ARGUMENTS in new terminal [duplicate]

This question already has answers here:
how do i start commands in new terminals in BASH script
(2 answers)
Closed 20 days ago.
So i want to open a new terminal in bash and execute a command with arguments.
As long as I only take something like ls as command it works fine, but when I take something like route -n , so a command with arguments, it doesnt work.
The code:
gnome-terminal --window-with-profile=Bash -e whoami #WORKS
gnome-terminal --window-with-profile=Bash -e route -n #DOESNT WORK
I already tried putting "" around the command and all that but it still doesnt work
You can start a new terminal with a command using the following:
gnome-terminal --window-with-profile=Bash -- \
bash -c "<command>"
To continue the terminal with the normal bash profile, add exec bash:
gnome-terminal --window-with-profile=Bash -- \
bash -c "<command>; exec bash"
Here's how to create a Here document and pass it as the command:
cmd="$(printf '%s\n' 'wc -w <<-EOF
First line of Here document.
Second line.
The output of this command will be '15'.
EOF' 'exec bash')"
xterm -e bash -c "${cmd}"
To open a new terminal and run an initial command with a script, add the following in a script:
nohup xterm -e bash -c "$(printf '%s\nexec bash' "$*")" &>/dev/null &
When $* is quoted, it expands the arguments to a single word, with each separated by the first character of IFS. nohup and &>/dev/null & are used only to allow the terminal to run in the background.
Try this:
gnome-terminal --window-with-profile=Bash -e 'bash -c "route -n; read"'
The final read prevents the window from closing after execution of the previous commands. It will close when you press a key.
If you want to experience headaches, you can try with more quote nesting:
gnome-terminal --window-with-profile=Bash \
-e 'bash -c "route -n; read -p '"'Press a key...'"'"'
(In the following examples there is no final read. Let’s suppose we fixed that in the profile.)
If you want to print an empty line and enjoy multi-level escaping too:
gnome-terminal --window-with-profile=Bash \
-e 'bash -c "printf \\\\n; route -n"'
The same, with another quoting style:
gnome-terminal --window-with-profile=Bash \
-e 'bash -c '\''printf "\n"; route -n'\'
Variables are expanded in double quotes, not single quotes, so if you want them expanded you need to ensure that the outermost quotes are double:
command='printf "\n"; route -n'
gnome-terminal --window-with-profile=Bash \
-e "bash -c '$command'"
Quoting can become really complex. When you need something more advanced that a simple couple of commands, it is advisable to write an independent shell script with all the readable, parametrized code you need, save it somewhere, say /home/user/bin/mycommand, and then invoke it simply as
gnome-terminal --window-with-profile=Bash -e /home/user/bin/mycommand

How I can write a command in a terminal which is running a program and I open from a script

I already know how to open a terminal in a bash with gnome-terminal and execute a program:
gnome-terminal -e ./OpenBTSCLI
But I also need that once open that program in the new terminal, write another command inside.
When a I tried to use echo, the message appear in the terminal where I run the bash.
I tried: gnome-terminal -e "bash -c './OpenBTSCLI && echo message'" that I find online but its not working, it only do the first part.
Anyone have an idea of how to resolve this? Thank you
I think it does the second command as well, but the new terminal closes as soon as the command's finished, so you don't see it. I reversed the order of quotes and added a 1s sleep at the end to allow seeing the echo.
gnome-terminal -e 'bash -c "./OpenBTSCLI && echo message && sleep 1"'

How can I start the gnome-terminal with multiple tabs with always the same title and history?

I want to start the bash with 4 tabs, having different titles. In all of them I'm working in different directories so it would be useful if I can cd to different paths.
Now it would be also great to save the history separately for every tab. So that every tab only remembers the commands I ran on it, even after a reboot.
Currently I have a script which starts gnome-terminal with 4 tabs.
gnome-terminal --geometry=150x50 --tab --title="src" -e "bash -c \"cd "~/path/to/src";exec bash\"" --tab --title="first test" -e "bash -c \"cd "~/path/to/single-test-dir";exec bash\"" --tab --title="test3" -e "bash -c \"cd "~/path/to/testdir";exec bash\"" --tab --title="test4" -e "bash -c \"cd "~/path/to/somewhere";exec bash\""
I suppose you can use a gnome-terminal custom command for each profile, for example
bash -c 'PROFILE=default_profile exec bash'
or
bash -c 'PROFILE=screen_profile exec screen -U'
or similar.
Then in ~/.bashrc
if [[ -n $PROFILE ]]; then
HISTFILE=~/.bash_history."$PROFILE"
fi
source of the answer.

Resources