3

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
4
  • Are you running it as scriptname or source 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 if SHLVL is 1. – Tom Zych Dec 8 '15 at 1:56
  • Using . but also finding that using return;; instead of exit will work – Michael Durrant Dec 8 '15 at 1:57
  • Yes, there you go. return only works in functions and sourced scripts, so if you're using . or source, use return. – Tom Zych Dec 8 '15 at 1:59
  • 1
    Why are you not running the script as ./scriptname (the easiest imHo) or bash scriptname. – user79743 Dec 8 '15 at 3:52
2

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.
0

With an script named scriptname.sh which contains only this:

#!/bin/bash
echo "script executed"
exit

Will make the shell where you are working close if the script is sourced.

To prevent that the whole window also close, start a new bash sub-shell by simply executing bash. The depth of sub-shells could be seen in the SHLVL variable:

$ echo $SHLVL
1
$ bash
$ echo $SHLVL
2
$ bash
$ echo $SHLVL
3

If at that point you source the script above:

$ source ./scriptname.sh
script executed
$ echo $SHLVL
2

As you can see, one instance of bash got closed.
The same will happen with .

$ . ./scriptname.sh
script executed
$ echo $SHLVL
1

If you source the script again at this level, the whole window will close. To avoid that call a new instance of bash.

A better way to run the program ./scriptname.sh is to make it executable:

$ bash
$ echo $SHLVL
2
$ chmod u+x scriptname.sh
$ ./scriptname.sh
script executed
$ echo $SHLVL
2

Or even calling the script with the name of the shell:

$ bash ./scriptname
script executed
$ echo $SHLVL
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.