Skip to main content
added 23 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

In Bourne/POSIX shells, the special parameter $0 is set to the name of the script that the shell executes (or the name of the shell itself). It does not change when reading another script file with the . (“dot”) command. Bash follows the standard behavior in this respect, including when using the source synonym of . (“dot”).

In zsh, by default the special parameter $0 is set to the path to the current script or the name of the current function, i.e. it changes when using the . or source builtin, when calling a function, and when returning from a function or script. You can change zsh to the standard behavior by settingunsetting the option function_argzero (which is set whenby default unless zsh is in sh or ksh emulation mode) or setting posix_argzero.

In bash, the path to the current script (affected by . or source) is available under the name $BASH_ARGV0.


Incidentally, since you know that you have a path to a non-directory file with a directory component, you can use the simple and slightly faster "${0%/*}" to refer to the directory containing it.1

1 It's also more robust than "$(dirname -- "$0")" because it works if the directory name ends with a newline, and than "$(dirname "$0")" because it works if the path starts with a dash, but neither of those apply here. It's more fragile because it doesn't work if the path ends with a slash or doesn't contain a slash at all, but that doesn't apply here either.

In Bourne/POSIX shells, the special parameter $0 is set to the name of the script that the shell executes (or the name of the shell itself). It does not change when reading another script file with the . (“dot”) command. Bash follows the standard behavior in this respect, including when using the source synonym of . (“dot”).

In zsh, by default the special parameter $0 is set to the path to the current script or the name of the current function, i.e. it changes when using the . or source builtin, when calling a function, and when returning from a function or script. You can change zsh to the standard behavior by setting the option function_argzero (which is set when zsh is in sh or ksh emulation mode) or posix_argzero.

In bash, the path to the current script (affected by . or source) is available under the name $BASH_ARGV0.


Incidentally, since you know that you have a path to a non-directory file with a directory component, you can use the simple and slightly faster "${0%/*}" to refer to the directory containing it.1

1 It's also more robust than "$(dirname -- "$0")" because it works if the directory name ends with a newline, and than "$(dirname "$0")" because it works if the path starts with a dash, but neither of those apply here. It's more fragile because it doesn't work if the path ends with a slash or doesn't contain a slash at all, but that doesn't apply here either.

In Bourne/POSIX shells, the special parameter $0 is set to the name of the script that the shell executes (or the name of the shell itself). It does not change when reading another script file with the . (“dot”) command. Bash follows the standard behavior in this respect, including when using the source synonym of . (“dot”).

In zsh, by default the special parameter $0 is set to the path to the current script or the name of the current function, i.e. it changes when using the . or source builtin, when calling a function, and when returning from a function or script. You can change zsh to the standard behavior by unsetting the option function_argzero (which is set by default unless zsh is in sh or ksh emulation mode) or setting posix_argzero.

In bash, the path to the current script (affected by . or source) is available under the name $BASH_ARGV0.


Incidentally, since you know that you have a path to a non-directory file with a directory component, you can use the simple and slightly faster "${0%/*}" to refer to the directory containing it.1

1 It's also more robust than "$(dirname -- "$0")" because it works if the directory name ends with a newline, and than "$(dirname "$0")" because it works if the path starts with a dash, but neither of those apply here. It's more fragile because it doesn't work if the path ends with a slash or doesn't contain a slash at all, but that doesn't apply here either.

Source Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k

In Bourne/POSIX shells, the special parameter $0 is set to the name of the script that the shell executes (or the name of the shell itself). It does not change when reading another script file with the . (“dot”) command. Bash follows the standard behavior in this respect, including when using the source synonym of . (“dot”).

In zsh, by default the special parameter $0 is set to the path to the current script or the name of the current function, i.e. it changes when using the . or source builtin, when calling a function, and when returning from a function or script. You can change zsh to the standard behavior by setting the option function_argzero (which is set when zsh is in sh or ksh emulation mode) or posix_argzero.

In bash, the path to the current script (affected by . or source) is available under the name $BASH_ARGV0.


Incidentally, since you know that you have a path to a non-directory file with a directory component, you can use the simple and slightly faster "${0%/*}" to refer to the directory containing it.1

1 It's also more robust than "$(dirname -- "$0")" because it works if the directory name ends with a newline, and than "$(dirname "$0")" because it works if the path starts with a dash, but neither of those apply here. It's more fragile because it doesn't work if the path ends with a slash or doesn't contain a slash at all, but that doesn't apply here either.