How to stop bash scripts from exit ing the window altogether
5,386
break
is what you are looking for.
exit
terminates the shell process when called. As you are sourcing your shell scripts, they are run within your current shell. This means when a sourced shell scripts hits an exit
it will terminate your shell.
break
on the other hand merely leaves the current loop structure, the while loop in your case.
From the bash manual:
break
break [n]
Exit from a for, while, until, or select loop. If n is supplied, the
nth enclosing loop is exited. n must be greater than or equal to 1.
The return status is zero unless n is not greater than or equal to 1.
Related videos on Youtube
Comments
-
Michael Durrant almost 2 years
When I write bash script and I have
exit;;
or
exit 0;;
the script not only exits but the window (or pane in the case of tmux panes) exits completely (goes away).
e.g.
while true; do read -p 'Run with -a (auto-correct) options?' yn case $yn in [Yy]* ) rubocop -a $@;; [Nn]* ) exit;; # <--here exits window completely ! * ) echo "Yes or No!";; esac done
How can I prevent this?
My bashrc is:
HISTCONTROL=ignoreboth:erasedups HISTSIZE=100000 HISTFILESIZE=200000 shopt -s histappend checkwinsize PROMPT_COMMAND='history -a' test -f ~/.bash_functions.sh && . $_ # I can comment these out & it doesn't help test -f ~/.bash_aliases && . $_ test -f ~/.eq_aliases && . $_ test -f ~/.git-completion.bash && . $_ test -f /etc/bash_completion && ! shopt -oq posix && . /etc/bash_completion test -f ~/.autojump/etc/profile.d/autojump.sh && . $_ ls --color=al > /dev/null 2>&1 && alias ls='ls -F --color=al' || alias ls='ls -G' HOST='\[\033[02;36m\]\h'; HOST=' '$HOST TIME='\[\033[01;31m\]\t \[\033[01;32m\]' LOCATION=' \[\033[01;34m\]`pwd | sed "s#\(/[^/]\{1,\}/[^/]\{1,\}/[^/]\{1,\}/\).*\(/[^/]\{1,\}/[^/]\{1,\}\)/\{0,1\}#\1_\2#g"`' BRANCH=' \[\033[00;33m\]$(git_branch)\[\033[00m\]\n\$ ' PS1=$TIME$USER$HOST$LOCATION$BRANCH PS2='\[\033[01;36m\]>' set -o vi # vi at command line export EDITOR=vim export PATH="/usr/local/heroku/bin:$PATH" # Added by the Heroku Toolbelt export PYTHONPATH=/usr/local/lib/python2.7/site-packages/ # for meld mdd 4/19/2014 [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" # friendly for non-text files [ ${BASH_VERSINFO[0]} -ge 4 ] && shopt -s autocd #[ `uname -s` != Linux ] && exec tmux export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting export PATH=$HOME/.node/bin:$PATH
-
Tom Zych over 8 yearsAre you running it as
scriptname
orsource scriptname
or. scriptname
? The latter two are equivalent and will run it in the shell itself, not as a subprocess, so yes,exit
will close the window ifSHLVL
is 1. -
Michael Durrant over 8 yearsUsing
.
but also finding that usingreturn;;
instead ofexit
will work -
Admin over 8 yearsWhy are you not running the script as
./scriptname
(the easiest imHo) orbash scriptname
.
-