{"id":2256,"date":"2020-02-11T18:46:36","date_gmt":"2020-02-11T13:16:36","guid":{"rendered":"https:\/\/opstree.com\/blog\/\/?p=2256"},"modified":"2020-02-24T18:07:32","modified_gmt":"2020-02-24T12:37:32","slug":"shell-initialization-files","status":"publish","type":"post","link":"https:\/\/opstree.com\/blog\/2020\/02\/11\/shell-initialization-files\/","title":{"rendered":"Shell initialization files"},"content":{"rendered":"\r\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\r\n\r\n\r\n\r\n<p>A shell initialization file is a shell script that runs automatically each time the shell executes. The initialization file sets up the &#8220;work environment&#8221; and &#8220;customizes&#8221; the shell environment for the user. The main agenda of Shell initialization files are to persist common shell configuration, such as<!--more-->:<\/p>\r\n\r\n\r\n\r\n<ul>\r\n<li>$PATH and other environment variables<\/li>\r\n<li>shell prompt<\/li>\r\n<li>shell tab-completion<\/li>\r\n<li>aliases, functions<\/li>\r\n<li>key bindings<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>Shell modes<\/strong><\/h2>\r\n\r\n\r\n\r\n<div class=\"wp-block-group\">\r\n<div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<p>Before understanding shell initialization files, first understand shell initialization files are dependent on the &#8220;combination of shell modes&#8221; in which a particular shell process runs. The shell can be run in three possible modes:<\/p>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<ul>\r\n<li>Interactive login\u00a0<\/li>\r\n<li>Interactive non-login\u00a0<\/li>\r\n<li>Non-interactive\u00a0<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>Here are some operations which result in the execution of different combination shell modes<\/strong><\/h2>\r\n\r\n\r\n\r\n<ul>\r\n<li>Login to a remote system via SSH : <strong>Login, Interactive<\/strong><\/li>\r\n<li>User successfully login into the system, using \/bin\/login, after reading credentials stored in the \/etc\/passwd file: <strong>Login, Interactive<\/strong><\/li>\r\n<li>Execute a script remotely and request a terminal, e.g. ssh user@host -t &#8216;echo $PWD&#8217; : <strong>Non-Login, Interactive<\/strong><\/li>\r\n<li>Start a new shell process, e.g. bash : <strong>Non\u2011Login, Interactive<\/strong><\/li>\r\n<li>Execute a script remotely, e.g. ssh user@host &#8216;echo $PWD : <strong>Non\u2011Login, Non\u2011Interactive<\/strong><\/li>\r\n<li>Run a script, bash myscript.sh: <strong>Non\u2011Login, Non\u2011Interactive<\/strong><\/li>\r\n<li>Run an executable with #!\/usr\/bin\/env bash shebang : <strong>Non\u2011Login, Non\u2011Interactive<\/strong><\/li>\r\n<li>Open a new graphical terminal window\/tab: <strong>Non\u2011Login, Interactive<\/strong><\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>Now, back to our shell initialization files execution based on shell modes.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>There are two types of shell initialization files\u00a0<\/strong><\/h2>\r\n\r\n\r\n\r\n<ul>\r\n<li>System-wide startup files<\/li>\r\n<li>User-specific startup files<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\"><strong>System-wide Startup Files<\/strong><\/h3>\r\n\r\n\r\n\r\n<p>These are the initialization files that contain configurations that applied to the whole system irrespective of a specific user, which means all users can share the same configuration which applied in system-wide startup files.\u00a0System-wide startup files are:<\/p>\r\n\r\n\r\n\r\n<ul>\r\n<li><strong>The \/etc\/profile file<\/strong> \u2013 It stores system-wide environment configurations and startup programs for login setup. All configurations that you want to apply to all system users\u2019 environments should be added in this file.<\/li>\r\n<li><strong>The \/etc\/bashrc<\/strong> <strong>or \/etc\/bash.bashrc file <\/strong>\u2013 It contains system-wide functions and aliases including other configurations that apply to all system users.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\"><strong>User-specific startup files<\/strong><\/h3>\r\n\r\n\r\n\r\n<p>These are the initialization files which contain configuration which applied to the specific user, means all users can have their own configuration which applied in user-specific startup files. User-specific startup files are located in home directory of the user and files are .profile, .bash_profile, .bashrc and .bash_login.<\/p>\r\n\r\n\r\n\r\n<ul>\r\n<li><strong>~\/.bash_profile file<\/strong> \u2013 Stores user-specific environment and startup programs configurations.\u00a0<\/li>\r\n<li><strong>~\/.bashrc file<\/strong> \u2013 Stores user-specific aliases and functions.<\/li>\r\n<li><strong>~\/.bash_login file <\/strong>\u2013 Contains specific configurations that are normally only executed when you log in to the system. When the ~\/.bash_profile is absent, this file will be read by bash.<\/li>\r\n<li><strong>~\/.profile file<\/strong> \u2013 It is read in the absence of ~\/.bash_profile and \u00a0 \u00a0 ~\/.bash_login; it can store the same configurations, which are can also be accessible by other shells on the system. Because we have mainly talked about bash here, take note that other shells might not understand the bash syntax.<\/li>\r\n<li><strong>~\/.bash_history file<\/strong> \u2013 Bash maintains a history of commands that have been entered by a user on the system. This list of commands is kept in a user\u2019s home directory in the ~\/.bash_history file.<\/li>\r\n<li><strong>~\/.bash_logout file<\/strong> \u2013 it\u2019s not used for shell startup, but stores user specific instructions for the logout procedure. It is read and executed when a user exits from an interactive login shell.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>Order of activation of system-wide startup files and user-specific startup files based on shell mode:<\/strong><\/h2>\r\n\r\n\r\n\r\n<p class=\"has-text-align-left\"><strong>1. Non-interactive mode:<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"has-text-align-left\">source file in $BASH_ENV<\/p>\r\n\r\n\r\n\r\n<p class=\"has-text-align-left\"><strong>2. Interactive login mode:<\/strong><\/p>\r\n\r\n\r\n\r\n<ul>\r\n<li>\/etc\/profile<\/li>\r\n<li>~\/.bash_profile, ~\/.bash_login, ~\/.profile (only first one that exist)<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"has-text-align-justify\"><strong>3. Interactive non-login mode:<\/strong><\/p>\r\n\r\n\r\n\r\n<ul>\r\n<li>\/etc\/bash.bashrc<\/li>\r\n<li>~\/.bashrc<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<div class=\"wp-block-image\">\r\n<figure class=\"alignright size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-2270\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2020\/02\/shell-initailizationfiles-1.png?w=1024\" alt=\"\" width=\"590\" height=\"315\" \/><\/figure>\r\n<\/div>\r\n\r\n\r\n\r\n<p>Now we have a good idea of what are all the shell modes, why shell initialization files are important and how the execution of shell initialization files depends on the different combination shell modes.<\/p>\r\n\r\n\r\n\r\n<p>With this knowledge, we can achieve modification to shell and access to users based on the shell mode.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">Reference:<\/h3>\r\n\r\n\r\n\r\n<p>Image &#8211; <a href=\"https:\/\/blog.flowblok.id.au\/2013-02\/shell-startup-scripts.html\" target=\"_blank\" rel=\"noopener\">https:\/\/blog.flowblok.id.au\/2013-02\/shell-startup-scripts.html<\/a><\/p>\r\n\r\n\r\n\r\n<p>Blog &#8211; <a href=\"https:\/\/www.tecmint.com\/understanding-shell-initialization-files-and-user-profiles-linux\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.tecmint.com\/understanding-shell-initialization-files-and-user-profiles-linux\/<\/a><\/p>\r\n\r\n\r\n\r\n<p>Opstree is an End to End DevOps solution provider<\/p>\r\n<p>\r\n\r\n<\/p>\r\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\r\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link\" title=\"https:\/\/www.opstree.com\/contact-us\" href=\"https:\/\/www.opstree.com\/contact-us\" target=\"_blank\" rel=\"noopener\">contact us<\/a><\/div>\r\n<\/div>\r\n<div class=\"wp-block-buttons\">\u00a0<\/div>\r\n","protected":false},"excerpt":{"rendered":"<p>Introduction A shell initialization file is a shell script that runs automatically each time the shell executes. The initialization file sets up the &#8220;work environment&#8221; and &#8220;customizes&#8221; the shell environment for the user. The main agenda of Shell initialization files are to persist common shell configuration, such as<\/p>\n","protected":false},"author":159459904,"featured_media":29900,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_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","enabled":false},"version":2}},"categories":[28070474,610],"tags":[694462508,694462444,212375717,694462454],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/11\/DevSecOps-1.jpg","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pfDBOm-Ao","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/2256"}],"collection":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/users\/159459904"}],"replies":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/comments?post=2256"}],"version-history":[{"count":25,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/2256\/revisions"}],"predecessor-version":[{"id":2420,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/2256\/revisions\/2420"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media\/29900"}],"wp:attachment":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media?parent=2256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/categories?post=2256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/tags?post=2256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}