Managing your Bash profiles on Linux

How to use .bash_profile and .bashrc configuration files to manage what happens when your bash shell starts. The snip explains the difference between both files and examples covering each use case. Both files can be used to store variables, aliases, functions, and any code that needs to get executed when the shell starts. Prerequisites include: The expected audience are Linux users who are comfortable with the bash shell. You should have access to a Linux bash shell, regardless of the Linux flavor. The examples in the snip were not tested on macOS machines so they may not work exactly the same for macOS users.

Hello everyone, If you log into your Linux host and drawn LS dash. A to show hidden files. You gotta find that you have a file that is called dot Bash RC another file. It's called either dot profile like this one or the bash underscore profile. So what do those files do and how can you benefit from them? Let's have a look? If I go 2 dot bashrc for example, and I go to the end of the file like this, I can say for example, I will get printed whenever the show starts. Let's save this file and let's see if that, really happens or not. I will log out of my shell. Log in again. Right as you can see here once I logged in. I will have this message. I will get printed whenever the shell starts OK, so once they good for any command that gets written here in block bash RC is gonna get executed whenever the shell starts. So I can for example, set an alias for myself. Let's say that I want this message to get printed. But I wanted to get printed using a shortcut so let's say greet. OK and that shortcut is going to trigger echo Welcome to Linux. OK, now let's me open another shell, and I now have access to this shortcut if I type greet OK. It's going to Welcome to Linux. So I can wear whatever commands or variables that I need to use in my shell by adding them in either the bash RC or 2 dot profile or to bash underscore profile. Let's have another example. Let's say that I want to variable that will be available to be throughout my session. So I can say my name equals a moment like this, let me open a new shell. And if I top echo my name. So I can add that to a script for example, or a command like. Like this so as you can see you can add whatever commands aliases functions or variables that you want to be available to you within your session through either. Dash RC or the profile, and sometimes it is named. Dot dash underscore profile. But there is a difference between both files. Let's say if I open another shell here like this and type. Echo dollar sign my name Gonna have my name that is because the bashRC is going to get executed whenever the login shell starts or any subsequent or child shell. But The BashRC script file works in a different way it only gets executed when the first session or as it's called the login shell is started so if I go to dot profile. This is going to be printed in the login shell only. Thanks, it. Got my exit from all my subshells an exit from the whole session. And log in again to mom next host this is called the login shell OK. This is going to be printed in the login shell only has been printed if I open another shell like this. It's not going to print it another one. It won't get printed. And well I do have access to the variable that has been specified in dot dash RC if I added a variable here like for example. OK then this is going to be only available to my login shell, so let's logout login. OK, if I hit enter I'm going to have the correct phrase. My name is Ahmed and I work as a dev OPS. Engineer was if I open another shell, which is the exact same command. I'm going to have only my name my name is Ahmed. I work as and the variable is empty that is because. It has been defined in the dot profile and our profile works only in the login. Shell and not in child or subsequent shells. I hope you enjoyed this snip. Thank you for watching.