Skip to content

Instantly share code, notes, and snippets.

@nmccready
Last active May 6, 2020 20:35
Show Gist options
  • Save nmccready/dbcef31c5acda5dff1d8a89b8ed2b0fe to your computer and use it in GitHub Desktop.
Save nmccready/dbcef31c5acda5dff1d8a89b8ed2b0fe to your computer and use it in GitHub Desktop.
check sourced or subshell bash
#!/usr/bin/env bash
function ohHi(){
echo "Hi"
}
(return 0 2>/dev/null) && return || echo "Script is a subshell"
# self invoke if not sourced
ohHi
@nmccready
Copy link
Author

$ ./sourceOrSubshell.sh
Script is a subshell
Hi

source example

$ source sourceOrSubshell.sh

$ ohHi
Hi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment