Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Detect if a script is being sourced vs executed
- X-seq: zsh-users 19072
- From: Daniel Hahler <dhahler@xxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Detect if a script is being sourced vs executed
- Date: Fri, 12 Sep 2014 19:57:19 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed;        d=gmail.com; s=20120113;        h=from:message-id:date:user-agent:mime-version:to:subject         :content-type:content-transfer-encoding;        bh=nfuBzy+GyJjuJxpL3X4tBy+41sm/OxAfKGfO0LhSl6w=;        b=Pj6dQ6XNoOOLeYDn1Zg+6osY9oxjaLT9a+MkHAPswA52RUSXmv0Fenh7XgHY7p+yIj         f+D2W5FXFFEwOM0qLEQvc/vqTmxYn8q1Z9noIMF3bJ8YMVBEUlAZFxOTGWS56+vH5Ywg         vhGvVNvUdzq5BmNYm+7D3MXJ/eGgAuT4WYAisD5BMejl1U/CcDqfosWvVwMrroj1zaHB         KAiBWW7RxwZ2zRH9B3Q2gPKy8kS0dc+B0KvOEstLDpTusCA2bITWI4CJXFZXAqMGnGHP         JDezaDVxcI3oOZpLD+jzaZX7u0M1IDQ/jSPMtMfm5wQ/5JPFsv4CzZzg3wL6Lk0qiwQ5         lv5w==
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
I want to detect if a script is being sourced instead of being called.
This is meant for pyenv, which uses shims to delegate execution to the selected Python version.
Currently it always uses "exec" to do so, but there is a pull request to add support for scripts that are meant to be sourced (like virtualenvwrapper.sh), and then would use "source" instead of "exec" in the shim.
The PR provides this functionality for Bash, which provides a way to detect this via $BASH_SOURCE/$BASH_LINENO: https://github.com/yyuu/pyenv/pull/100/files
My current approach so far is the following, which requires Zsh 5.0.5 (for POSIX_ARGZERO) and does not work for "zsh test_source.sh" yet (a patch has been posted to zsh-workers to fix this).
I would like to do this for older Zsh versions, and in a simpler / more elegant way.
Script to be sourced (test_source.sh):
    #!/usr/bin/zsh
    echo "\$0: $0"
    sourced=0
    if [ -n "$ZSH_VERSION" ]; then
      # Use prompt expansion to get the current file.
      cur_file=${(%):-%x}
      # Fix $0:
      if (( ${+options[posixargzero]} )); then  # added in zsh 5.0.5 (2014-06-01)
        if [[ $options[posixargzero] != "on" ]]; then
          setopt posixargzero
          [ "$0" = "$cur_file" ] || sourced=1
          setopt noposixargzero
        else
          [ "$0" = "$cur_file" ] || sourced=1
        fi
      else
        echo "TODO"
      fi
    elif [ -n "$BASH_VERSION" ]; then
      [ "$0" = "$BASH_SOURCE" ] || sourced=1
    fi
    echo "sourced: $sourced"
Test script (test.sh):
    #!/usr/bin/zsh
    echo "== CALL"
    ./test_source.sh
    echo "== SOURCE"
    . ./test_source.sh
    echo "== CALL: bash"
    bash ./test_source.sh
    echo "== CALL: zsh"
    zsh ./test_source.sh
    echo "== EXEC"
    exec ./test_source.sh
There must be an easier way!?
It would be really useful, if Zsh would provide a mechanism like Bash to simplify this.
Regards,
Daniel.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author