{"id":870,"date":"2013-08-23T12:53:22","date_gmt":"2013-08-23T20:53:22","guid":{"rendered":"https:\/\/accretiondisc.com\/blog\/?p=870"},"modified":"2013-08-23T12:59:09","modified_gmt":"2013-08-23T20:59:09","slug":"bash-initialization","status":"publish","type":"post","link":"https:\/\/accretiondisc.com\/blog\/2013\/08\/23\/bash-initialization\/","title":{"rendered":"Bash Initialization"},"content":{"rendered":"<p>Every time I monkey with my <code>.profile<\/code> or <code>.bashrc<\/code> file I regret it.(<code>.profile<\/code> is what graybeards like me use instead of using <code>.bash_profile<\/code> like all the young people who won&#8217;t stay off my lawn.)<\/p>\n<p>If you&#8217;re like me, you get those confused all the time, so let&#8217;s go to the <em>bash<\/em>(1) man page. (Amazingly, it is a man page and <a href=\"http:\/\/xkcd.com\/912\/\">not an info node<\/a>. Ahem.) Here&#8217;s the salient bit of that 37,000 word tome:<\/p>\n<blockquote><p>When bash is invoked as an interactive login shell, or as a non-interactive shell with the <em>&#8211;login<\/em> option, it first reads and executes commands from the file <code>\/etc\/profile<\/code>, if that file exists. After reading that file, it looks for <code>~\/.bash_profile<\/code>, <code>~\/.bash_login<\/code>, and <code>~\/.profile<\/code>, in that order, and reads and executes commands from the first one that exists and is readable. The <em>&#8211;noprofile<\/em> option may be used when the shell is started to inhibit this behavior.<\/p>\n<p>When a login shell exits, bash reads and executes commands from the files <code>~\/.bash_logout<\/code> and <code>\/etc\/bash.bash_logout<\/code>, if the files exists.<\/p>\n<p>When an interactive shell that is not a login shell is started, bash reads and executes commands from <code>~\/.bashrc<\/code>, if that file exists. This may be inhibited by using the <em>&#8211;norc<\/em> option. The <em>&#8211;rcfile<\/em> file option will force bash to read and execute commands from file instead of <code>~\/.bashrc<\/code>.<\/p>\n<p>When bash is started non-interactively, to run a shell script, for example, it looks for the variable <code>BASH_ENV<\/code> in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:<\/p>\n<p><code><\/p>\n<pre>if [ -n \"$BASH_ENV\" ]; then . \"$BASH_ENV\"; fi<\/pre>\n<p><\/code><\/p>\n<p>but the value of the <code>PATH<\/code> variable is not used to search for the file name.\n<\/p><\/blockquote>\n<p>Got all that? The key is that your <code>.profile<\/code> is read first for login shells and <code>.bashrc<\/code> for non-login shells. There&#8217;s no reason why you should ever worry about what order they get invoked in, because there&#8217;s no reason to invoke them both at all. Except if you want to observe the <a href=\"http:\/\/www.artima.com\/intv\/dry.html\">DRY principle<\/a>. In which case they should both invoke a separate startup file of your own design.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every time I monkey with my .profile or .bashrc file I regret it.(.profile is what graybeards like me use instead of using .bash_profile like all the young people who won&#8217;t stay off my lawn.) If you&#8217;re like me, you get those confused all the time, so let&#8217;s go to the bash(1) man page. (Amazingly, it [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[52],"tags":[781,782,784,783,215,171],"class_list":["post-870","post","type-post","status-publish","format-standard","hentry","category-technology","tag-bash","tag-bashrc","tag-dry","tag-initialization","tag-profile","tag-unix"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paRqpr-e2","_links":{"self":[{"href":"https:\/\/accretiondisc.com\/blog\/wp-json\/wp\/v2\/posts\/870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/accretiondisc.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/accretiondisc.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/accretiondisc.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/accretiondisc.com\/blog\/wp-json\/wp\/v2\/comments?post=870"}],"version-history":[{"count":0,"href":"https:\/\/accretiondisc.com\/blog\/wp-json\/wp\/v2\/posts\/870\/revisions"}],"wp:attachment":[{"href":"https:\/\/accretiondisc.com\/blog\/wp-json\/wp\/v2\/media?parent=870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/accretiondisc.com\/blog\/wp-json\/wp\/v2\/categories?post=870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/accretiondisc.com\/blog\/wp-json\/wp\/v2\/tags?post=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}