- Source .profile using bash?
- 2 Answers 2
- You must log in to answer this question.
- Related
- Hot Network Questions
- Subscribe to RSS
- Where to Source your .Profile (Ubuntu 21.10)
- 3 Answers 3
- How to Reload .bash_profile in Linux Command-Line
- The .bash_profile File
- Creating the .bash_profile File
- Edit the .bash_profile File
- Customizing the .bash_profile File
- Reload .profile in bash shell script (in unix)?
- 5 Answers 5
Source .profile using bash?
I have a script that puts a new alias into my ~/.profile from command line. I have to source it subsequently. It would be really convenient if the script itself can source it.
First off, ~/.profile is the wrong place to put aliases, they should be in ~/.bashrc . Secondly, aliases don’t work in scripts; you have to specifically enable aliases in scripts. Unless you source your script from an interactive shell, those alias settings will have no effect.
Can you please elaborate why ~/.profile is a wrong place to put aliases. I read it somewhere on internet and it really works for me. Is there any specific diadvantage of doing so?
~/.profile only gets sourced by interactive login shells, not interactive non-login shells. See mywiki.wooledge.org/DotFiles
2 Answers 2
That script itself must be sourced for it to make changes to the current shell. If you run the script, a new instance of bash is started to interpret the commands in the script. This new instance cannot alter its parent, thus any aliases that are set in the script, dies with the script.
@ChiragVora Either have the user source the script instead of running it, or make it a function (which must be put in ~/.bashrc first).
I’m way too late for this party but actually I was having exactly the same problem today. I came by to find a solution but what I found kept me thinking «this can’t be the workaround to this».
So I went again to Terminator’s options and the way I solved it was just by clicking a check-mark control inside the Terminator preferences. Just by checking the «Run command as a login shell» and then restarting Terminator, I was able to run «rails console» or «rails server» without having to source no file.
screenshot for the Terminator’s preference window :
You must log in to answer this question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.17.43537
Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Where to Source your .Profile (Ubuntu 21.10)
Recent instructional video I was watching said to put your export PATH=»$PATH. » statement in your .profile and after sourcing manually in the terminal to append «. ~/.profile» to your .bashrc file as well. However, when I do this it messes up my terminal when I open a new tab rather than allows me to access the bin from the path. Did this behavior change? If so, where is the correct place to source my profile so that opening a new tab or terminal window gives access to the same commands/executables in the bin folder I am referencing? Mainly need guidance on ensuring that changes to .profile did not need to be referenced elsewhere. This was understood and picked up by the person who answered my question — marked as answer below. Edited for clarity and to fix a typo.
3 Answers 3
Ubuntu has a non-standard setup it has inherited from Debian and its .bashrc file is sourced by .profile :
$ grep -A1 bashrc /etc/skel/.profile # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi
So if you then also source .profile from ~/.bashrc you get into an infinite loop, which will break your shell.
The right way to add a new directory to your $PATH is to edit ~/.profile . The file is sourced every time you start a new login shell and, on Ubuntu systems, is also sourced when you log in graphically. After modifying ~/.profile to add something to the $PATH , the change will take effect next time you log in. So instead of making ~/.bashrc source ~/.profile , just logout and log back in again and your changes will be there.
The answer is: nowhere. .profile is sourced automatically each time you log in. .bashrc is sourced each time an interactive terminal starts. There is no use in trying to source .profile once more in .bashrc , which actually causes an infinite loop on Ubuntu because .bashrc is sourced in .profile .
So far with respect to the problem you ask about, problem Y.
With respect to your real problem, X, i.e., having a bin folder in your PATH : no other action is needed than 1) creating ~/bin (and/or ~/.local/bin ), then 2) log out and back in. If these directories exist, they are automatically added to your PATH (you can see the code that does that in ~/.profile ). To keep things neat, do not add other directories to your PATH, although you could do so in your .profile file.
export PATH=$PATH’::’ appends paths to the PATH environment variable. It is the paths that should be used when locating executable files (programs).
If you have a program in /home//bin called ‘Gloop’, and you type Gloop at the command prompt in the bash shell (terminal), it normally won’t execute, because /home//bin is not in the PATH variable, so bash doesn’t know to look there for programs names.
So, you would go up to the first line, and insert /home//bin with your actual username, in place of , and then you can type Gloop at the command line, and it will launch.
But sometimes you want to execute a program, but you don’t want it in the PATH.
Then, you do ./Gloop from the directory the program is in. Why you would want to append the contents of .Profile to the PATH is beyond me. It’s usually empty, so the effect would be nothing!
If you want to make your changes to the PATH environment variable persistent across reboots, put the export command, and everything else you need from the first line of this answer, and append it to the end of the file /home//.bashrc
.bashrc holds your personal bash environment. It is sourced each time you log in. Try taking another look at the video, and see what the author is trying to do with .Profile
How to Reload .bash_profile in Linux Command-Line
The Linux operating system environment is famed for numerous OS-based attributes. One of them is the Linux home directory. It is responsible for all user profiles in the system and enables the system users to create and store files or access already existing/pre-defined system tools and resources.
The .bash_profile File
Since the .bash_profile file is defined in the Linux home directory, the system bash shell is dependent on it and other system files like /etc/profile, .bash_login, .bash_history, and .bash_logout to self-initialize.
For custom configuration of all Linux user environments, you need to be able to access and edit the .bash_profile configuration file. You can either change this file’s default settings or add new ones.
The .bash_profile file serves two primary objectives:
- Modification of the Linux operating system working environment by configuring and editing terminal settings, and custom environment variables.
- Provision of applications initialization instructions to the operating system.
Creating the .bash_profile File
First, create a .bash_profile file in your home directory if it doesn’t exist and open it with your favorite editor.
$ touch .bash_profile $ nano .bash_profile
Once the file has been created, we need to check for the existence of this created file. Since we created it with the ‘.’ prefix, it will be hidden. Exit the nano editor (Ctrl+x) and run the following command:
As you can see, several other bash files exist alongside it.
Edit the .bash_profile File
Use the nano editor to re-open this file so that we can start making some edits.
We will populate our newly created .bash_profile file with some code. We will use the echo command to populate this file with a one-line code to make this tutorial interesting.
$ echo 'LinuxShellTips tutorial on reloading the .bash_pofile file'
Save this file to implement the changes made to it and exit the nano editor.
To activate the changes made, we will need to reload the .bash_profile file from the Linux terminal. Reloading this file requires the use of the “source” command.
$ source .bash_profile or $ source ~/.bash_profile
The latter command gives you the flexibility to reload the
file if you are on a different OS environment path and not directly on the Linux home directory.
Customizing the .bash_profile File
Let us open and modify the .bashrc file we viewed from the ls -la command.
At the bottom of this file, add an echo statement with the following details:
echo "This is a bashrc file"
We now want to link this .bashrc file to the .bash_profile file we created and modified earlier. Re-open the .bash_profile file.
At the bottom of the file, add the following code.
# Getting aliases and functions if [-f ~/.bashrc]; then . ~/.bashrc fi
The above code on the .bash_profile file will check for the existence of the .bashrc file and then try to reload it. Let us reload the .bash_profile file and see the outcome.
We have successfully linked the .bash_profile and .bashrc files and executed .bashrc from .bash_profile . The phrase “This is a bashrc file” will be appearing each time you open a new terminal window.
Open the .bash_profile again.
Let us set and export a system path variable with the following code:
PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH
Save the file and exit the nano editor.
Now, echo the $PATH variable from the command line.
We have successfully set a system path variable and accessed it from the echo command.
Mastering the use of the .bash_profile file can give a system user flexibility like composing a welcome terminal message (This is a bashrc file) and even associating variable paths ($PATH) with the system user directories. There is more to it if you dig deeper.
Reload .profile in bash shell script (in unix)?
I’m new to bash shell scripting, and have come across a challenge. I know I can reload my «.profile» file by just doing:
but I’m trying to execute the same in a bash script I’m writing and it is just not working. Any ideas? Anything else I can provide to clarify? Thanks
As Ignacio points out below, your script is running in a subshell. The subshell can’t modify the main shell (the child process can’t modify its parent). So you need to «source» the script by using the «.» command (which can also be spelled as «source»). So if your script wants to, say, modify environment variables, you need to do something like «source myscript» or «. myscript» (they both mean the same thing). This will modify your main shell’s environment. (Which I think is what you’re trying to do, let me know if this is wrong.)
5 Answers 5
Try this to reload your current shell:
Do you have an alternate solution ? Because using the source command will run the file as a script. In worst cases, if somebody would use a variable assignment like MyVar=»$foo$MyVar» in their bash_profile, then source ~/.profile would give the end result MyVar=»$foo$MyVar$MyVar» , hence $MyVar would have the wrong value afterwards. (Regardless of bad practices, just ask for an alternate solution)
A couple of issues arise when trying to reload/source ~/.profile file. [This refers to Ubuntu linux — in some cases the details of the commands will be different]
- Are you running this directly in terminal or in a script?
- How do you run this in a script?
Running this directly in terminal means that there will be no subshell created. So you can use either two commands:
In both cases this will update the environment with the contents of .profile file.
Ad 2) You can start any bash script either by calling
In the first case this will create a subshell that will not affect the environment variables of your system and they will be visible only to the subshell process. After finishing the subshell command none of the exports etc. will not be applied. THIS IS A COMMON MISTAKE AND CAUSES A LOT OF DEVELOPERS TO LOSE A LOT OF TIME.
In order for your changes applied in your script to have effect for the global environment the script has to be run with
In order to make sure that you script is not runned in a subshel you can use this function. (Again example is for Ubuntu shell)
I hope this clears some of the common misunderstandings! 😀 Good Luck!