{"id":11986,"date":"2019-03-20T16:47:54","date_gmt":"2019-03-20T16:47:54","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11986"},"modified":"2019-03-20T16:47:54","modified_gmt":"2019-03-20T16:47:54","slug":"understanding-shell-initialization-files-and-user-profiles-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/20\/understanding-shell-initialization-files-and-user-profiles-in-linux\/","title":{"rendered":"Understanding Shell Initialization Files and User Profiles in Linux"},"content":{"rendered":"<p>Linux is a multi-user, time sharing system, implying that more than one user can log in and use a system. And system administrators have the task of managing various aspects of how different users can operate a system in terms of\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-package-management\/\" target=\"_blank\" rel=\"noopener noreferrer\">installing\/updating\/removing software<\/a>, programs they can run, files they can view\/edit and so on.<\/p>\n<p>Linux also allows users\u2019 environments to be created or maintained in two major ways: using system-wide (global) and user-specific (personal) configurations. Normally, the basic method of working with a Linux system is the shell, and the shell creates an environment depending on certain files it reads during its initialization after a successful user login.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/set-unset-environment-variables-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Set Environment Variables in Linux<\/a><\/p>\n<p>In this article, we will explain shell initialization files in relation to user profiles for local user management in Linux. We will let you know where to keep custom shell functions, aliases, variables as well as startup programs.<\/p>\n<p><strong>Important<\/strong>: For the purpose of this article, we will focus on\u00a0<strong>bash<\/strong>, a\u00a0<strong>sh<\/strong>\u00a0compatible shell which is the\u00a0<a href=\"https:\/\/www.tecmint.com\/different-types-of-linux-shells\/\" target=\"_blank\" rel=\"noopener noreferrer\">most popular\/used shell on Linux systems<\/a>\u00a0out there.<\/p>\n<p>If you are using a different shell (zsh, ash, fish etc..) program, read through its documentation to find out more about some of the related files we will talk about here.<\/p>\n<h3>Shell Initialization in Linux<\/h3>\n<p>When the shell is invoked, there are certain initialization\/startup files it reads which help to\u00a0<a href=\"https:\/\/www.tecmint.com\/set-unset-environment-variables-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">setup an environment for the shell<\/a>\u00a0itself and the system user; that is predefined (and customized) functions, variables, aliases and so on.<\/p>\n<p>There are two categories of initialization files read by the shell:<\/p>\n<ul>\n<li><strong>system-wide startup files<\/strong>\u00a0\u2013 theses contain global configurations that apply to all users on the system, and are usually located in the\u00a0<strong>\/etc<\/strong>\u00a0directory. They include:\u00a0<strong>\/etc\/profiles<\/strong>\u00a0and\u00a0<strong>\/etc\/bashrc<\/strong>\u00a0or\u00a0<strong>\/etc\/bash.bashrc<\/strong>.<\/li>\n<li><strong>user-specific startup files<\/strong>\u00a0\u2013 these store configurations that apply to a single user on the system and are normally located in the users home directory as dot files. They can override the system-wide configurations. They include:\u00a0<strong>.profiles<\/strong>,\u00a0<strong>.bash_profile<\/strong>,\u00a0<strong>.bashrc<\/strong>\u00a0and\u00a0<strong>.bash_login<\/strong>.<\/li>\n<\/ul>\n<p>Again, the shell can be invoked in three possible modes:<\/p>\n<h4>1. Interactive Login Shell<\/h4>\n<p>The shell is invoked after a user successfully login into the system, using\u00a0<strong>\/bin\/login<\/strong>, after reading credentials stored in the\u00a0<strong>\/etc\/passwd<\/strong>\u00a0file.<\/p>\n<p>When the shell is started as an interactive login shell, it reads the\u00a0<strong>\/etc\/profile<\/strong>\u00a0and its user-specific equivalent\u00a0<strong>~\/.bash_profile<\/strong>.<\/p>\n<div id=\"attachment_25429\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/interactive-login-shell.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25429\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/interactive-login-shell.png\" alt=\"Linux Interactive Login Shell\" width=\"485\" height=\"197\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Linux Interactive Login Shell<\/p>\n<\/div>\n<h4>2. Interactive non-login Shell<\/h4>\n<p>The shell is started at the command-line using a shell program for example\u00a0<strong>$\/bin\/bash<\/strong>\u00a0or\u00a0<strong>$\/bin\/zsh<\/strong>. It can as well be started by running the\u00a0<strong>\/bin\/su<\/strong>\u00a0command.<\/p>\n<p>Additionally, an interactive non-login shell can as well be invoked with a terminal program such as\u00a0<strong>konsole<\/strong>,\u00a0<a href=\"https:\/\/www.tecmint.com\/terminator-a-linux-terminal-emulator-to-manage-multiple-terminal-windows\/\" target=\"_blank\" rel=\"noopener noreferrer\">terminator<\/a>\u00a0or\u00a0<a href=\"https:\/\/www.tecmint.com\/install-guake-terminal-ubuntu-mint-fedora\/\" target=\"_blank\" rel=\"noopener noreferrer\">xterm<\/a>\u00a0from within a graphical environment.<\/p>\n<p>When the shell is started in this state, it copies the environment of the parent shell, and reads the user-specific\u00a0<strong>~\/.bashrc<\/strong>\u00a0file for additional startup configuration instructions.<\/p>\n<pre>$ su\r\n# ls -la\r\n<\/pre>\n<div id=\"attachment_25430\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/su-command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25430\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/su-command.png\" alt=\"Interactive Non-Login Shell\" width=\"703\" height=\"306\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Interactive Non-Login Shell<\/p>\n<\/div>\n<h4>3. Non-interactive Shell<\/h4>\n<p>The shell is invoked when a shell script is running. In this mode, it\u2019s processing a script (set of shell or generic system commands\/functions) and doesn\u2019t require user input between commands unless otherwise. It operates using the environment inherited from the parent shell.<\/p>\n<h3>Understanding System-wide Shell Startup Files<\/h3>\n<p>In this section, we will shade more light on shell startup files that store configurations for all users on the system and these include:<\/p>\n<p>The\u00a0<strong>\/etc\/profile file<\/strong>\u00a0\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.<\/p>\n<p>For instance, you can set your the global PATH environment variable here.<\/p>\n<pre># cat \/etc\/profile\r\n<\/pre>\n<div id=\"attachment_25431\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/etc-profile-file.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25431\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/etc-profile-file.png\" alt=\"System Wide Configuration File\" width=\"703\" height=\"591\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">System Wide Configuration File<\/p>\n<\/div>\n<p><strong>Note<\/strong>: In certain systems like\u00a0<strong>RHEL\/CentOS 7<\/strong>, you\u2019ll get such warnings as \u201cIt\u2019s not recommended to change this file unless you know what you are doing. It\u2019s much better to create a custom\u00a0<strong>.sh<\/strong>\u00a0shell script in\u00a0<strong>\/etc\/profile.d\/<\/strong>\u00a0to make custom changes to your environment, as this will prevent the need for merging in future updates\u201d.<\/p>\n<p>The\u00a0<strong>\/etc\/profile.d\/<\/strong>\u00a0directory \u2013 stores shell scripts used to make custom changes to your environment:<\/p>\n<pre># cd \/etc\/profile.d\/\r\n# ls  -l \r\n<\/pre>\n<div id=\"attachment_25432\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/ls-l-custom-scripts.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25432\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/ls-l-custom-scripts.png\" alt=\"Stores Custom Shell Scripts\" width=\"600\" height=\"363\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Stores Custom Shell Scripts<\/p>\n<\/div>\n<p>The\u00a0<strong>\/etc\/bashrc<\/strong>\u00a0or\u00a0<strong>\/etc\/bash.bashrc<\/strong>\u00a0file \u2013 contains system-wide functions and aliases including other configurations that apply to all system users.<\/p>\n<p>If your system has\u00a0<a href=\"https:\/\/www.tecmint.com\/different-types-of-linux-shells\/\" target=\"_blank\" rel=\"noopener noreferrer\">multiple types of shells<\/a>, it is a good idea to put bash-specific configurations in this file.<\/p>\n<pre># cat \/etc\/bashrc\r\n<\/pre>\n<div id=\"attachment_25433\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/etc-bashrc-file.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25433\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/etc-bashrc-file.png\" alt=\"System Wide Functions and Aliases\" width=\"702\" height=\"648\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">System Wide Functions and Aliases<\/p>\n<\/div>\n<h3>Understanding User-specific Shell Startup Files<\/h3>\n<p>Next, we will explain more concerning user-specific shell (bash) startup dot files, that store configurations for a particular user on the system, they are located in a user\u2019s home directory and they include:<\/p>\n<pre># ls -la\r\n<\/pre>\n<div id=\"attachment_25434\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/User-Specific-Configuration-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25434\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/User-Specific-Configuration-Files.png\" alt=\"User Specific Configuration Files\" width=\"700\" height=\"477\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">User Specific Configuration Files<\/p>\n<\/div>\n<p>The\u00a0<strong>~\/.bash_profile<\/strong>\u00a0file \u2013 this stores user specific environment and startup programs configurations. You can set your custom PATH environment variable here, as shown in the screenshot below:<\/p>\n<pre># cat ~\/.bash_profile\r\n<\/pre>\n<div id=\"attachment_25435\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/User-Bash-Profile.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25435\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/User-Bash-Profile.png\" alt=\"User Bash Profile\" width=\"518\" height=\"306\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">User Bash Profile<\/p>\n<\/div>\n<p>The\u00a0<strong>~\/.bashrc<\/strong>\u00a0file \u2013 this file stores user specific aliases and functions.<\/p>\n<pre># cat ~\/.bashrc\r\n<\/pre>\n<div id=\"attachment_25436\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/User-Bashrc-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25436\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/User-Bashrc-File.png\" alt=\"User Bashrc File\" width=\"418\" height=\"268\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">User Bashrc File<\/p>\n<\/div>\n<p>The\u00a0<strong>~\/.bash_login<\/strong>\u00a0file \u2013 it contains specific configurations that are normally only executed when you log in to the system. When the\u00a0<strong>~\/.bash_profile<\/strong>\u00a0is absent, this file will be read by bash.<\/p>\n<p>The\u00a0<strong>~\/.profile<\/strong>\u00a0file \u2013 this file is read in the absence of\u00a0<strong>~\/.bash_profile<\/strong>\u00a0and\u00a0<strong>~\/.bash_login<\/strong>; 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.<\/p>\n<p>Next, we will also explain two other important user specific files which are not necessarily bash initialization files:<\/p>\n<p>The\u00a0<strong>~\/.bash_history<\/strong>\u00a0file \u2013 bash maintains a\u00a0<a href=\"https:\/\/www.tecmint.com\/history-command-examples\/\" target=\"_blank\" rel=\"noopener noreferrer\">history of commands<\/a>\u00a0that have been entered by a user on the system. This list of commands is kept in a user\u2019s home directory in the\u00a0<strong>~\/.bash_history<\/strong>\u00a0file.<\/p>\n<p>To view this list, type:<\/p>\n<pre>$ history \r\nor \r\n$ history | less\r\n<\/pre>\n<div id=\"attachment_25438\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/List-Last-Executed-Commands.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25438\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/List-Last-Executed-Commands.png\" alt=\"View Last Executed Commands\" width=\"680\" height=\"230\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">View Last Executed Commands<\/p>\n<\/div>\n<p>The\u00a0<strong>~\/.bash_logout<\/strong>\u00a0file \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.<\/p>\n<p>One practical example would by clearing the terminal window upon logout. This is important for remote connections, which will leave a clean window after closing them:<\/p>\n<pre># cat bash_logout \r\n<\/pre>\n<div id=\"attachment_25439\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/Clear-History-After-Logout.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25439\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/04\/Clear-History-After-Logout.png\" alt=\"Clear History After Logout\" width=\"376\" height=\"116\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Clear History After Logout<\/p>\n<\/div>\n<p>For additional insights, checkout the contents of these shell initialization files on various Linux distros and also read through the bash man page.<\/p>\n<p>&nbsp;<br \/>\n<a href=\"https:\/\/www.tecmint.com\/understanding-shell-initialization-files-and-user-profiles-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux is a multi-user, time sharing system, implying that more than one user can log in and use a system. And system administrators have the task of managing various aspects of how different users can operate a system in terms of\u00a0installing\/updating\/removing software, programs they can run, files they can view\/edit and so on. Linux also &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/20\/understanding-shell-initialization-files-and-user-profiles-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Understanding Shell Initialization Files and User Profiles in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11986","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11986"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11986\/revisions"}],"predecessor-version":[{"id":11987,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11986\/revisions\/11987"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}