[SOLVED] Is there a way to force the terminal to execute a command?

rado84

Well-Known Member
Joined
Feb 25, 2019
Messages
782
Reaction score
638
Credits
4,964
For some time now gnome-terminal seems to be ignoring the command "exit". If you just open the terminal and type "exit", that does close the terminal. But if it's like "[your command] && [your command] && exit", the terminal ignores "&& exit" and it remains open after the execution of "your command". I asked on Gnome's forum ( https://discourse.gnome.org/t/gnome...it-open-whereas-others-dont-why-is-that/16991) but there's still no answer and I doubt there will be, so I'm thinking about workarounds. Something that would tell the terminal that it's mandatory to execute the "exit". Some of you probably know that in CSS if you write "!important" after a string, that "!important" means "mandatory". So I was wondering if there's a terminal equivalent of that "!important" thing. Something like "!exit", maybe. So, is there such a thing in terminal?
 


IIRC,
& = execute the following command.
&& = execute the following command IF the previous command was successful.

In your example should any of the previous commands fail, exit will not execute.
 
I'll try with a single ampersand but the thing is that it remains open, even if the previous command was successful.

Edit: with a single ampersand it just hangs there like it's expecting a user input. With a double ampersand it remains open, despite that the previous command was successful. Looks like there's no exit from this situation - pun intended.
 
A double ampersand says to run the following command only if the preceding command succeeded. A single ampersand says to unconditionally run the preceding command in the background. You might use it when starting a GUI application from the terminal but you don't want the terminal to be tied up while the GUI app runs. For instance:
Code:
firefox &

# or, using both to start the browser only if you have internet access..

wget perdu.com && firefox &
 
A single ampersand runs the command as a job in the background. That's not going to help you.
The single ampersand is good for running GUI applications from the terminal, or running lengthy, non-interactive scripts in the background - to prevent the running process from blocking your terminal.

If you're in an interactive terminal session and you type the following one liner:
Bash:
echo -e "goodbye!\n" && sleep 2 && exit
Then your terminal should write "goodbye! " to the screen, before sleeping for 2 seconds and should then exit. This should end your interactive session and close the terminal.

The only time exit won't be called in the above command is if the sleep command somehow fails - which it shouldn't.

But if your second command is failing for whatever reason, then your're better off doing something like this:
Bash:
echo -e "goodbye!\n" && sleep 2; exit
That should run the exit command regardless of the success or failure of any of the previous commands. So if you put a semicolon ; between your second and third commands, the third command will be ran no matter what happens.

Another consideration is if you put the exit command inside a script:
e.g.
Bash:
#!/usr/bin/env bash
echo -e "goodbye!\n" && sleep 2 && exit
When you run the script the exit command will only exit the sub-shell that is running the script - it won't quit your main interactive terminal session.
If you want to end the main terminal session after running your script, you'd have to invoke your script like this instead:
Bash:
./path/to/script; exit
 
Also, if you have a background job running in the terminal, the shell might decline to exit but should give you a message to that effect:
Code:
tc@almighty:~$ # just run something in the background...
tc@almighty:~$ read JUNK >/dev/null 2>&1 &
tc@almighty:~$ # there's a message about the stopped job
[1]+  Stopped (tty input)        read JUNK 1>/dev/null 2>&1
tc@almighty:~$
tc@almighty:~$ # exit will fail bit give a message as to why
tc@almighty:~$ exit
You have stopped jobs.
tc@almighty:~$
 
For some time now gnome-terminal seems to be ignoring the command "exit". If you just open the terminal and type "exit", that does close the terminal. But if it's like "[your command] && [your command] && exit", the terminal ignores "&& exit" and it remains open after the execution of "your command". I asked on Gnome's forum ( https://discourse.gnome.org/t/gnome...it-open-whereas-others-dont-why-is-that/16991) but there's still no answer and I doubt there will be, so I'm thinking about workarounds. Something that would tell the terminal that it's mandatory to execute the "exit". Some of you probably know that in CSS if you write "!important" after a string, that "!important" means "mandatory". So I was wondering if there's a terminal equivalent of that "!important" thing. Something like "!exit", maybe. So, is there such a thing in terminal?
Its a long shot but check your preferences. You can create the symptoms by selecting it in the drop down box. see attached
 

Attachments

  • terminal.PNG
    terminal.PNG
    136.8 KB · Views: 107
Its a long shot but check your preferences

Indeed, worth a check and toggling and restarting.

The above screenshot is actually the default for gnome-terminal, but in itself does not work that way by default, go figure.

@rado84 Mate I have run tests on 6 or 8 of my Linux stable

Code:
sudo apt update && sudo apt upgrade && exit

works under my

gnome-terminal (both GNOME and Cinnamon DEs)
xfce4-terminal
mate-terminal
konsole

HTH

Chris
 
That's exactly the way I've set it - to exit the terminal once the command is executed, but it still didn't. I posted the same topic in Arch Linux's forum and there they helped me solve the problem, like this:

Code:
alias [aliasname]="exec /path/to/the/script.sh"

So now I'm adding an "exec" in front of the path of scripts whose commands don't close the terminal upon completion. That still doesn't explain why the terminal closes after some commands but doesn't after other commands. I'm wondering if the reason is because some scripts have quotations in them (to escape space in dir names).
 
I have not tried this.
1. Use a subshell:
By wrapping your commands in a subshell, you can ensure that the exit command is executed within that subshell. Once the subshell exits, the parent shell will continue executing any subsequent commands.
Code:
(your-command && another-command) && exit

2. Explicitly call the shell's exit:
Instead of just exit, you can try:
Code:
your-command && another-command && bash -c "exit"

3. Use the trap command:
The trap command allows you to specify commands that will be executed when a signal is received. You can set a trap to exit when the commands are done.
Code:
trap exit EXIT; your-command && another-command

Remember, the exit command in a terminal is used to end the shell session. If for some reason the shell session isn't ending, it could be due to a variety of reasons, including background processes still running, or terminal preferences/settings that override the default behavior. If none of the above solutions work, it might be worth investigating if there are any background processes or jobs that are preventing the terminal from closing.
 

Staff online

Members online


Top