how to properly exit bash?

interpretive language scripts


Moderator: Forum moderators

Post Reply
geo_c
Posts: 2609
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 1877 times
Been thanked: 745 times

how to properly exit bash?

Post by geo_c »

I've been doing a lot of scripting and improved my skills quite a bit, but one problem I'm running into is understanding the differences between exiting a script vs exiting a terminal or shell.

I've written a nice set of bash functions that test input from read commands and use return to exit the script back to a prompt, and it works well in those sourced function instances.

But, I have other scripts, some that I've tried to setup to launch from different places such as launching the executable script with [scriptname] from an open terminal vs using a launch script which opens a terminal and calls [scriptname] script. The latter I've written so they can be clicked to run from a file manager, or called from a desktop file (though I can also use the terminal=true method in a desktop file.)

In working these two scenarios I began exiting some scripts with the bash command, to keep the terminal open as opposed to exit which closes the terminal.

The truth is I just don't understand the workings of the shell, subshell, script interaction, and that has gotten me into trouble.

After ediiting and repeatedly running these scripts from a single terminal instance on two occassions, I seem to get some kind of loop going and my cpu starts racing, the temperature pops up hot, the desktop becomes mostly unresponsive, and I have to do a hard shutdown. I can not say for certain it's these scripts causing that, but my gut says it is.

Could someone point me to an easy way to understand when and how to use the various script/terminal exit commands?

geo_c
Old School Hipster, and Such

williwaw
Posts: 1695
Joined: Tue Jul 14, 2020 11:24 pm
Has thanked: 151 times
Been thanked: 310 times

Re: how to properly exit bash?

Post by williwaw »

ctrl-c will stop a running command in a terminal session
typing bash opens a new shell in the session, but does not end something running in the existing shell

ctrl-d will end the terminal session
if you logged on from a terminal, you opened a new shell as the logon user. in this case ctrl-d will log you out as that user

Last edited by williwaw on Mon May 27, 2024 2:13 am, edited 1 time in total.
geo_c
Posts: 2609
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 1877 times
Been thanked: 745 times

Re: how to properly exit bash?

Post by geo_c »

williwaw wrote: Mon May 27, 2024 2:01 am

ctrl-c will stop a running command in a terminal session
ctrl-d will end the terminal session
typing bash opens a new shell in the session, but does not end something running in the existing shell

Yes, ctrl-c and d work to manually close the terminal, but what I'm trying to figure out is the way to do it in a script.

But since you have said typing bash opens a new shell, then maybe this is what's happening in my scripts. I read a lot fo stack-exchange posts talking about using bash to exit the script.

geo_c
Old School Hipster, and Such

williwaw
Posts: 1695
Joined: Tue Jul 14, 2020 11:24 pm
Has thanked: 151 times
Been thanked: 310 times

Re: how to properly exit bash?

Post by williwaw »

but what I'm trying to figure out is the way to do it in a script.

did the script initially open the terminal emulator?

Burunduk
Posts: 245
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 123 times

Re: how to properly exit bash?

Post by Burunduk »

geo_c wrote: Sun May 26, 2024 11:59 pm

using a launch script which opens a terminal and calls [scriptname] script.

I'm using this launcher to run a script in a terminal. It's an excerpt from a bigger script that asks for confirmation before launching anything from the rox-filer. The terminal window stays open with an active bash session when the script finishes. The script name is in the bash history, so the up-arrow key can be used to recall it. The launcher expects a full path to a script (unless it's in the PATH) as its single argument. It could be simpler, but I wanted it to handle script names with spaces and quotation marks.

Code: Select all

#!/bin/bash

printf -v list " clear; cd %q; %q; history -s %q; exec </dev/tty" "${1%/*}" "$1" "$(printf "%q" "$1")"
exec urxvt -title "Running: ${1##*/}" -e bash -c "bash -i <<< ${list@Q}"
Tippe
Posts: 140
Joined: Wed Feb 15, 2023 11:55 pm
Has thanked: 8 times
Been thanked: 21 times

Re: how to properly exit bash?

Post by Tippe »

Yes, ctrl-c and d work to manually close the terminal, but what I'm trying to figure out is the way to do it in a script.

Xdotool is able to send such commands within a script file.

Bionic Puppy 64bit.
Won't use another one.

User avatar
fredx181
Posts: 2684
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 297 times
Been thanked: 1058 times
Contact:

Re: how to properly exit bash?

Post by fredx181 »

Below should work nicely in many cases (I did mostly focus on 'how possibly best to 'force' (program or script) running from inside a terminal', perhaps not exactly on topic though), some examples from what I tested:
(saved below code as "runinterm" and placed it in PATH , e.g. in /usr/bin/):
- runinterm somescript(if somescript (or someprogram) is in the PATH)
- runinterm /path/to/somescript or with spaces in path (put quotes around it): runinterm "/path with spaces/to/somescript"
- runinterm cat /path/to/mytext.txt (or works too with e.g. added -n argument for cat runinterm cat -n /path/to/mytext.txt
- runinterm cat -n "/path with spaces/to/mytext.txt" (with spaces in path)
- runinterm apt update (if apt is installed, e.g. on Debian)
etc...

Usage cases examples:
To run from a .desktop launcher, make the Exec=... line e.g. Exec=runinterm myprogram or e.g. Exec=runinterm /path/to/myscript (edit: if runinterm is in PATH, e.g. in /usr/bin/))
Or set it up as a right-click option from file-manager.

Code: Select all

#!/bin/bash

[ -n "$(command -v xterm)" ] && export _TERM=xterm || export _TERM=rxvt

C=''
for i in "$@"; do
    i="${i//\\/\\\\}"
    C="$C \"${i//\"/\\\"}\""
done
export C
#echo "$C"

run_in_term () {
bash -c "$C"
bash   # change 'bash' to 'exit' if you want the terminal to be closed after the above command has been finished
}; export -f run_in_term

$_TERM -T "run_in_term" -si -sb -fg white -bg SkyBlue4 -geometry 80x16 -e bash -c run_in_term
User avatar
MochiMoppel
Posts: 1145
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 19 times
Been thanked: 377 times

Re: how to properly exit bash?

Post by MochiMoppel »

@fredx181 I suggest to shorten your code to a one-liner (e.g. no need to test for xterm or rxvt since in Puppies eventually they both will run urxvt):

Code: Select all

#! /bin/bash
urxvt -T "run_in_term" -si -sb -fg white -bg SkyBlue4 -geometry 80x16 -e bash -c "$@"

Your examples should still work, though different quoting and even escaping may be necessary, e.g.
runinterm "/path with spaces/to/somescript" would require an escaped space: runinterm "/path with\ spaces/to/somescript"

Other than that it seems to be more flexible and allows commands like this:
runinterm 'while :;do printf "%(%H:%M:%S)T\r"; sleep 1;done' (runs an endless loop in the terminal, showing a clock)

@geo_c To answer your question how to properly exit bash: Normally you don't have to do anything. When a bash sub shell is finished with the stuff it is supposed to do, it closes and it returns to the calling shell,

Consider this example:
urxvt -e bash -c "date" Instructs urxvt to run bash, tells bash to run the 'date' command, and when both are finished urxvt exits and it went so fast that you didn't see anything. But very often the command takes longer, e.g. a wget download, and to have the terminal window closed automatically is exactly what you want.

If you do want to see the output of the date command in above example and prevent the immediate closing you could
- use the -hold option: urxvt -hold -e bash -c "date"
- or add another bash command: urxvt -e bash -c "date;read -N1"
In the latter case bash would wait for keyboard input. It's the famous "Press any key" solution because the urxvt window will close as soon as you hit any key. If instead of read -N1 you prefer to open another bash shell (urxvt -e bash -c "date;bash") you can continue to use the urxvt window like a normal terminal window, but in this case you would have to close the new bash shell by typing exit, followed by Enter.

User avatar
fredx181
Posts: 2684
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 297 times
Been thanked: 1058 times
Contact:

Re: how to properly exit bash?

Post by fredx181 »

MochiMoppel wrote: Tue May 28, 2024 9:06 am

@fredx181 I suggest to shorten your code to a one-liner (e.g. no need to test for xterm or rxvt since in Puppies eventually they both will run urxvt):

Code: Select all

#! /bin/bash
urxvt -T "run_in_term" -si -sb -fg white -bg SkyBlue4 -geometry 80x16 -e bash -c "$@"

Your examples should still work, though different quoting and even escaping may be necessary, e.g.
runinterm "/path with spaces/to/somescript" would require an escaped space: runinterm "/path with\ spaces/to/somescript"

Yes, with your oneliner that works with escaped space(s), but not with the cat example e.g. runinterm cat "/pathwith\ space/to/mytext.txt"
Also I wanted to avoid the need for such tricks as escaping. Just quotes only, exactly the same as you can do normally when using path with space.

Other than that it seems to be more flexible and allows commands like this:
runinterm 'while :;do printf "%(%H:%M:%S)T\r"; sleep 1;done' (runs an endless loop in the terminal, showing a clock)

That's nice though. Indeed doesn't work with my script.
Well... probably you can't make it work in all cases, I guess.

User avatar
MochiMoppel
Posts: 1145
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 19 times
Been thanked: 377 times

Re: how to properly exit bash?

Post by MochiMoppel »

fredx181 wrote: Tue May 28, 2024 10:52 am

Yes, with your oneliner that works with escaped space(s), but not with the cat example e.g. runinterm cat "/pathwith\ space/to/mytext.txt"

Works with
runinterm "cat /pathwith\ space/to/mytext.txt" or runinterm "cat '/pathwith space/to/mytext.txt'"
I like the latter better. Would also work when passing filespecs which *might* contain spaces:
f='/pathwith space/to/mytext.txt' ; runinterm "cat '$f'"

geo_c
Posts: 2609
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 1877 times
Been thanked: 745 times

Re: how to properly exit bash?

Post by geo_c »

Thanks everyone, I will be trying out all these approaches in the next few weeks.

geo_c
Old School Hipster, and Such

Post Reply

Return to “Scripts”