Customize how Bash operates when it's opened. Find out how to modify the .bash_profile and .bashrc files.
- [Instructor] When Bash gets started Bash reads some startup files to, say, initialize some variables. And there's a couple of those you can have in your home directory that you can use to customize settings in Bash. One of them is .bash_profile, that's read just when Bash is started when you log in. And the file .bashrc is executed every time a new shell is started. So there's some things that are appropriate in Bash profile, and some things that are appropriate in .bashrc.
For example, setting environment variables are typically done in .bash_profile, not in .bashrc. If you set an environment variable to a value of the current value of the variable plus a new value that means the variable grows. And if you had that in your .bashrc, if you nested shells, if in a shell you called another shell, or in a shell program you called another shell program, that means that variable would continue to grow.
And that might not be what you want. Since exported variables are copied into new processes anyway, normally you just set those in your .bash_profile. But certain things are not exported like aliases, so if you want to define an alias and have it available in every shell you normally set that in your .bashrc. Setting Path in your bash profile is pretty common, to add some extra directories. So the syntax, if we're assigning a variable right as variable equals value.
And if you want the current value of the variable you say $ variable. And the PATH variable is a list of directories separated by colons. And colon is not a valid part of a shell variable. So when you say $PATH:/usr/local/bin, it's smart enough to know the variable name is p-a-t-h PATH. And it's just gonna concatenate the current value of PATH to a :usr/local/bin. Because aliases and functions are not normally exported, you would define those in your .bashrc.
And those aren't growing like PATH=$, PATH value would be. So let's look at the use of .bash_profile and .bashrc, let's start by looking at them. So tilde is a shortcut for my home directory. We can look at .bash_profile, and it's pretty short. It actually looks to see if there's a .bashrc in my home directory and runs that. And it sets the path variable to include its current value and adds in a couple of bin directories that could be underneath my home directory.
And exports PATH just in case PATH wasn't already exported. It doesn't hurt to export a variable that's already exported. If we look at .bashrc we see it looks for a file called bashrc in etc a system one. And if it's there it runs that, so a system administrator, for example, could have a bashrc file for all the users. Now let's look at a variable called xyz. Doesn't have a value, there's no variable xyz.
Let's edit our .bashrc and let's export xyz=$xyz plus some more stuff. We echo xyz, it's still not set because we didn't start a new shell. If we start a new shell, and we look at xyz, we see it got a value now. And if we start another new shell, and we look at xyz, we see it's growing.
We have a stack of shells here. If we exit out of this one, we go back to the previous one, xyz with just one value, we exit out of this one back to the first one, and there was no xyz here right. An exported variable gets copied to a new shell. And when we start a new shell the .bashrc is executed and if we assign a variable of value in there then that new shell gets that value.
- Exploring the Bash documentation
- Setting the script interpreter and permissions
- Sourcing and aliasing with bash
- Displaying text with echo
- Using the typeset and declare commands for variables
- Reading input
- Defining functions
- Using operators
- Using filters and parameter expansion
- Advanced bash scripting techniques