From 07632ba5a24d7af0ea6d5360cef6929c2f593500 Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Sat, 15 Oct 2022 00:22:46 +0200 Subject: [PATCH] Added file load order for Bash --- knowledge base/bash.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/knowledge base/bash.md b/knowledge base/bash.md index 35d5c4c..026d898 100644 --- a/knowledge base/bash.md +++ b/knowledge base/bash.md @@ -23,6 +23,28 @@ echo ${name,,} echo ${name^^} ``` +## Files loading order + +On startup: + +1. (if login shell) `/etc/profile` +1. (if interactive and non login shell) `/etc/bashrc` +1. (if login shell) `~/.bash_profile` +1. (if login shell and `~/.bash_profile` not found) `~/.bash_login` +1. (if login shell and no `~/.bash_profile` nor `~/.bash_login` found) `~/.profile` +1. (if interactive and non login shell) `~/.bashrc` + +Upon exit: + +1. (if login shell) `~/.bash_logout` +1. (if login shell) `/etc/bash_logout` + +## Check if a script is sourced by another + +```sh +(return 0 2>/dev/null) && echo "this script is not sourced" || echo "this script is sourced" +``` + ## Further readings - [Trap] @@ -31,10 +53,14 @@ echo ${name^^} ## Sources - [The Bash trap command] +- [Bash startup files loading order] +- [How to detect if a script is being sourced] [trap]: trap.md +[bash startup files loading order]: https://youngstone89.medium.com/unix-introduction-bash-startup-files-loading-order-562543ac12e9 +[how to detect if a script is being sourced]: https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced#28776166 [the bash trap command]: https://www.linuxjournal.com/content/bash-trap-command [upper- or lower-casing strings]: https://scriptingosx.com/2019/12/upper-or-lower-casing-strings-in-bash-and-zsh/