How to detect if a script is being sourced

Robust solutions for bash, ksh, zsh, including a cross-shell one, plus a reasonably robust POSIX-compliant solution: Version numbers given are the ones on which functionality was verified – likely, these solutions work on much earlier versions, too – feedback welcome. Using POSIX features only (such as in dash, which acts as /bin/sh on Ubuntu), there … Read more

What is the difference between $(command) and `command` in shell programming?

The backticks/gravemarks have been deprecated in favor of $() for command substitution because $() can easily nest within itself as in $(echo foo$(echo bar)). There are other differences such as how backslashes are parsed in the backtick/gravemark version, etc. See BashFAQ/082 for several reasons to always prefer the $(…) syntax. Also see the POSIX spec … Read more