2

Edit: If I can use return instead, what about functions that use set -e?

I run screen on my Linux VM in order to have multiple terminals that I can disconnect from and return to when I want.

I'm having issues with running bash commands that use functions that either directly call exit or have set -e in them - they cause the screen terminal to close and kick me out to another one of my screen terminals.

Is there a way to get screen to stop responding to exit calls, or perhaps make it aware that the exit is purely for the return value of the function that's using it?

The offending function:

function unpackdb() {
  cd $DB_LOCATION
  mkdir -p downloads
  cd downloads
  db_file_name=$(ls -t *.tar.gz 2> /dev/null | head -1)
  if [ -n "$db_file_name" ] ; then
    tar xf $db_file_name -C ../data
    echo "Latest file (${db_file_name}) restored in to ${pwd}/../data"
  else
    echo "Could not find a .tar.gz in ${pwd}, if you don't care use command 'startdb'"
    exit 1
  fi
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Joe Shanahan
  • 816
  • 5
  • 21
  • 1
    I understand the the function is defined in current shell environment. Replace exit with **return** and it will do the trick. – Jay jargot Mar 15 '16 at 17:47
  • 2
    @Joe: See: [Difference between return and exit in BASH functions](http://stackoverflow.com/q/4419952/3776858) – Cyrus Mar 15 '16 at 18:02
  • 1
    `function exit() { echo "no exit"; }` – Cyrus Mar 15 '16 at 18:06
  • @AlexWeitz Not "terminal". "Shell instance"/"Shell session". The terminal is the program giving you the window and running the shell. – Etan Reisner Mar 15 '16 at 18:16
  • 1
    You can also open a new shell session inside your original session by writing "bash" before running the command. You'll then be kicked out to your previous shell session @EtanReisner thanks, fixed. – Alex Weitz Mar 15 '16 at 18:19
  • If I can use `return` instead, what about functions that use `set -e`? – Joe Shanahan Mar 16 '16 at 09:08
  • Suspicion... given that **unpackdb()** function quoted above erroneously used **exit 1** (instead of **return 1**), do these other functions even need a `set -e`? Maybe a code snippet using `set -e` would help clarify things. – agc Apr 05 '16 at 05:06
  • The issue was that `set -e` is very useful, however you can't use it whilst running the function in the current shell of the screen terminal. – Joe Shanahan Apr 06 '16 at 09:00

0 Answers0