Skip to content

Instantly share code, notes, and snippets.

@AfroThundr3007730
Last active February 21, 2023 21:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AfroThundr3007730/6f3b146548b29a93b7097c10c02ae970 to your computer and use it in GitHub Desktop.
Save AfroThundr3007730/6f3b146548b29a93b7097c10c02ae970 to your computer and use it in GitHub Desktop.
Making executable scripts source-friendly
# This file can be sourced and also executed directly
# Definitions and declarations go here
function main () {
# All action happens here
return 0
}
# Only run if not sourced
if(!($MyInvocation.InvocationName -eq '.')) {
main $args
}
#!/usr/bin/python
# This file can be sourced and also executed directly
# Definitions and declarations go here
def main():
# All action happens here
return 0
# Only run if not sourced
if __name__ == '__main__':
main()
#!/bin/bash
# This file can be sourced and also executed directly
# Definitions and declarations go here
main () {
# All action happens here
return 0
}
# Only run if not sourced
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
main "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment