5

I have the following script:

#!/bin/bash

set -e

TITLE="Choose version"
VERSIONS=$(cat <<'END'
AAA
BBB
END
)

VERSION_LIST=$(echo "$VERSIONS" | awk '{print NR, " ", $0}')
INDEX=$(whiptail \
        --no-cancel \
        --menu "$TITLE" 15 40 15 \
        $VERSION_LIST \
        3>&1 1>&2 2>&3)

When I make it executable and run it, it works as intended. When I call it with source from bash or zsh, one of the following scenarios happens:

  1. Terminal closes (bash seems to exit)
  2. Terminal stays open, but will crash/exit soon. Easiest way to reproduce is to type ls somefile and press TAB for completion.

I'm using Fedora 29 and I also can see this behaviour on Mac OS. Is there any specific consideration that needs to be taken into account when using whiptail/dialog in a sourced script?

1 Answer 1

10

Your script sets the errexit flag with set -e. After you source it, unchecked failing commands will cause the shell to exit. If you're using the programmable completion scripts, some of them might run a failing command, which would explain why tab-completion triggers it.

The solution here would be to not source the script, but run it as usual, or to replace set -e with sufficient error checking on all the relevant commands, and to return if they fail. (Not exit, since that would exit the whole shell, return returns from a sourced script.)

1
  • Thank you! The script is being sourced because I want to set environment variables based on user's choice. Commented Feb 13, 2019 at 11:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .