I have the following files to handle shell configuration:
#~/.bash_profile
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
and
#~/.bashrc
... configure shell
If I open VSCode from the command line using code, my .bashrc is loaded whenever I add a new instance of the integrated shell.
However if I open VSCode via its icon, only my .profile is loaded.
How can I ensure my .bashrc is loaded instead?
I've tried various settings for the terminal.integrated.shellArgs.osx setting without any luck.
Simply add shell arguments to the VsCode settings.json file.
Paths to the settings.json file are as follows:
Windows: C:\Users\<username>\AppData\Roaming\Code\User\settings.json`
Linux: $HOME/.config/Code/User/settings.json
Mac: $HOME/Library/Application\ Support/Code/User/settings.json
Add one of the following:
"terminal.integrated.shellArgs.windows": ["-l"],
"terminal.integrated.shellArgs.linux": ["-l"],
"terminal.integrated.shellArgs.osx": ["-l"],
This will launch your shell of choice with the login argument. This will thus execute any user profile that is setup.
VSCode has deprecated "terminal.integrated.shellArgs.osx" in favor of using profiles. This does the trick for bash in osx. Omit the first line if you don't want bash to be your default profile:
"terminal.integrated.defaultProfile.osx": "bash",
"terminal.integrated.profiles.osx": {
"bash": {
"path": "bash",
"args": ["-l"]
}
}
Another possible solution that just worked for me. The settings.json file (whcih you can access in File > Preferences > Settings > Features > terminal > Integrated > Automation Shell: Linux) had a parameter
"terminal.integrated.inheritEnv": false
set to false by default. Changing it to true fixed the problem in my case.
I had the same problem with the Intellij Idea terminal on a Mac, the solution is the same for both. In settings change the path to the integrated terminal to "/bin/bash". Hope that helps.
You could also try the following:
1 Create a file named /usr/local/bin/bash-login and add :
#!/bin/bash
bash -l
2 Run:
chmod +x /usr/local/bin/bash-login
to make it executable.
3 On your VSC user settings add
{ "terminal.integrated.shell.osx": "/usr/local/bin/bash-login" }
The solution was described at https://github.com/Microsoft/vscode/issues/7263.
Hope it helps
"terminal.integrated.shellArgs.osx": ["-l"] is deprecated.
I personally wanted to use .bash_profile, so I made a .bashrc with this:
if [ -f ~/.bashrc ]; then
source ~/.bash_profile
fi
And then I had to do a full computer restart (just restarting VS code didn't work).
Bash will load various DotFiles sequentially:
~/.bash_profile
~/.bash_login
~/.profile
If ~/.bash_profile been loaded, the second and third one will not be loaded.
If ~/.bash_profile is not loaded, bash will find second one. If ~/.bash_login been loaded, the third one will not be loaded.
If ~/.bash_profile and ~/.bash_login are both not loaded, Bash will try to load ~/.profile file.
A fresh Ubuntu installation will only contains ~/.profile file. So I think it will be better NOT to use the ~/.bash_profile to avoid issues happening. Just use the ~/.profile file. Then your VSCode won't need to config anything.
I was having this issue too. But in my case I was on windows with Visual Studio code opening a remote dev env inside my CentOS WSL.
So fixing the configuration of this use case was a bit different.
In the IDE open settings. Then at the top select "Remote [WSL: XXX]"
The scroll down to Integrated -> Profiles: Linux
And click edit in settings.json
Then I added the following to the file:
"terminal.integrated.defaultProfile.linux": "bash",
"terminal.integrated.profiles.linux": {
"bash": {
"path": "bash",
"args": ["-l"]
}
}
Save the settings file and the next terminal you open will respect your ~/.bash_profile.
NOTE: my ~/.bash_profile had the same lines that someone else recommended adding to load the bashrc file in it already.
Related
Until recently, when I've launched the integrated terminal in Visual Studio Code, bash runs both my .bashrc and my .bash_profile files. But now it's only running the .bashrc file. I need it to run both.
Some details: VSC 1.70.1 (latest), with the "Remote - SSH" extension, running under Windows 10 (updated) with WSL 2 installed. When I launch a Microsoft terminal, it runs both .bashrc and .bash_profile, but VSC's integrated terminal only runs the former. This probably means I have s.t. wrong in my VSC config, but the settings there seem to change frequently and it's hard to keep up. The relevant parts seem to be
"terminal.integrated.defaultProfile.windows": "WSL",
"terminal.integrated.profiles.windows": {
"bash":{
"path": "C:\\Windows\\System32\\bash.exe",
"args": ["-l"]
},
"WSL": {
"path": "C:\\WINDOWS\\System32\\wsl.exe",
"args": [ ],
"icon": "terminal-ubuntu"
}
},
but that doesn't work, nor do any of the variants I've tried on the two "args" parameters, nor changing the defaultProfile to "bash" instead of "WSL".
Before I give in and put all my startup settings in my .bashrc file and get rid of my .bash_profile file, what do I yet lack?
The historical presumption on UNIX is that .bash_profile runs once when you log in, then .bashrc runs in each new shell. Thus, things that are inherited by child processes (like environment variables) go in .bash_profile so they can only run once per login (often, as a parent process to xinit or otherwise GUI startup), whereas things that need to be separately configured for each new shell (like aliases, non-exported functions, prompt settings, etc) go in .bashrc.
If, however, you're in an environment where .bash_profile isn't run during session creation, you might want to invoke it during .bashrc operation (so it happens during setup of any new interactive shell). Consider adding the following to your .bash_profile:
# put this at the top of your .bash_profile
[[ $profile_already_sourced ]] && return
declare -x profile_already_sourced=1 # export so we only run once per process tree
# ...set up your environment variables here...
[[ -s ~/.bashrc ]] && source ~/.bashrc
...and the following to your .bashrc:
# put this at the top of your .bashrc
[[ $bashrc_already_sourced ]] && return
declare -- bashrc_already_sourced=1 # do not export this!
# ...do your non-exported shell-local configuration here...
[[ -s ~/.bash_profile ]] && source ~/.bash_profile
...that way you can have each file source the other when needed without causing an infinite loop.
I can't reproduce this after even after upgrading to 1.70.1, so I'd recommend trying out these things:
try
"path": "C:\\WINDOWS\\System32\\wsl.exe",
"args": ["-e", "bash", "-li"]
to make sure that a login shell is started
if this changed after upgrading, read Help -> Release Notes to find out what might be responsible, e.g. the Shell integration is now enabled by default, so you might try "terminal.integrated.shellIntegration.enabled": false
sync / backup your settings, re-install vanilla VSC, check if behaviour is still the same
If I start a new integrated terminal in VScode it does not seem to load my .bashrc file. Once the terminal is open I can source ~/.bashrc and the custom settings then appear. My problem seems to be a duplicate of this question, however for windows instead of osx.
Is it possible to have vscode automatically source my .bashrc or .bash_profile when it starts a new instance of the integrated terminal?
So far i have tried the following:
I have pointed the terminal to git bash by setting "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
I have tried pass a shell argument in the settings, using "terminal.integrated.shellArgs.windows": ["-l"] however it is not clear in the documentation if this applies to windows or just linux.
Adding this to my settings.json fixed this issue for me.
"terminal.integrated.shellArgs.windows": [
"-l"
]
Edit: Derp I am bad at reading. It might work for you if you move your .bashrc content into a .bash_profile file.
I am trying to create a permanent alias for my terminal. I put the alias in my ~/.profile, ~/.bashrc, and ~/.bash_profile files, previously empty. When I start a new terminal, bash does not recognize the alias, but if I source any of them, it does. Why are these not getting run when I open a terminal? I am on OSX.
Newer MacOS versions use zsh as the default shell for both Terminal and iTerm2. Run echo $SHELL to confirm if this is the case for you.
Zsh looks for a .zshrc file upon shell startup, so to continue using zsh while sourcing the contents of your bash profile, you can run the following:
echo "source ~/.bash_profile" >> ~/.zshrc
Open a new terminal window for the changes to take effect.
Two things need to happen here when using iTerm to get the loading of dotfiles to work.
First you should add the following to your .bash_profile
[[ -s ~/.bashrc ]] && source ~/.bashrc
Secondly you will need to ensure that in iTerm preferences your terminal is set to launch a login shell.
Hope this helps!
Using the default mac terminal, what worked for me was to add a command to run on start up to source my .bash_profile.
Preferences > Profile > Startup > Add command 'source ~/.bash_profile'
Mac terminal preferences window screenshot
Might be considered to be a bit hacky, but it does the trick.
Adding source ~/.profile to my .bash_profile worked for me.
As of High Sierra, both Terminal and iTerm want to load ~/.profile first. So I suggest you put one line in your .profile to make your Mac work like other Unixes:
source ~/.bash_profile
By editing this one file, you won't have to search through the menus of multiple apps to override Apple's bizarre behavior in each.
As of Catalina the default shell is now zsh. You can change it back to bash with chsh -s /bin/bash and that should load your .profile or .bash_profile
Why are your shell's initialization files not loading?
As with most things, It Depends ™
I recently experienced the same phenomenon and went through the following exercise to resolve it:
I use iTerm. iTerm runs a login shell by default. Verify in iTerm Preferences > General > Command > (*) Login Shell
Therefore, I know that ~/.bash_profile will always be called.
Knowing that, I put the following in my ~/.bash_profile file:
for file in ~/.{bashrc,bash_exports,bash_aliases,bash_functions}; do
[ -r "$file" ] && source "$file"
done
unset file
Notice that I use separate files for .bashrc, .bash_exports, etc. It keeps things separate and simple.
Note also that /etc/profile is loaded first, but since I have never used that system wide init file, I knew that that was not my problem. For details check out $ man bash
So, I started with my ~/.bash_profile file.
I found that when I installed Canopy Express that it's installer replaced the contents of my ~/.bash_profile file with the following content:
# Added by Canopy installer on 2017-04-19
# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make the bash prompt show that Canopy is active, otherwise 1
alias activate_canopy="source '/Users/lex/dev/python/User/bin/activate'"
# VIRTUAL_ENV_DISABLE_PROMPT=1 source '/Users/lex/dev/python/User/bin/activate'
p.s. Canopy is an excellent, free python IDE, that I highly recommend.
Fortunately, I backup my ~/.bash* files so restoring that was easy and quickly fixed my issue.
My advice would be to understand the order of calls to your initialization files and start with the first one and work your way through them until you find the problem.
Oh, and you may want to verify which shell you are using (I use bash):
~ $ echo $SHELL
/usr/local/bin/bash
I am guessing you may use another shell, such as bash, tcsh, sh, zsh etc.
Put source .bash_profile into your appropriate 'bashrc' file will make the auto loading recovered, i.e.
.login for tcsh, .bash_profile for bash, .zshrc for zsh
My issue was solved by unchecking Preferences > General > tmux >
Use "tmux" profile rather than profile of the connecting session
Most likely, you need to create the files yourself as they appear not to exist by default. You should give them execute permission to make them run.
~ % sudo chmod 700 ~/.bash_profile
Also, you should check the ownership of the files. They should belong to current user rather than root. Otherwise, you will get permission denied error.
~ % ls -a -l
~ % sudo chown <user_name> ~/.bash_profile
Finally, please note that bash looks in your home directory for .bash_profile, .bash_login, and .profile in order. Bash will stop looking if the first is found.
This means if you have both .bash_profile and .profile files, the .profile will not run.
For more information
Hope this would help you.
Little late to the party but it seems that the file .zprofile is the equivalent to that of .bash_profile when loading zsh. I used this instead to execute a few commands on startup. Of course this only valid for a specific iTerm setup with zsh.
https://zsh.sourceforge.io/Intro/intro_3.html
So I'm installing some things for coding and personal usage, and I need to run this in the terminal (I'm on Mac if you didn't read the title).
~/.bash_profile
It just says permission denied, Im running OSX 10.8.4 Mountain Lion. How do I bypass this?
On MacOS: add source ~/.bash_profile to the end of ~/.zshrc.
Then this profile will be in effect when you open zsh.
You would never want to run that, but you may want to source it.
. ~/.bash_profile
source ~/.bash_profile
both should work. But this is an odd request, because that file should be sourced automatically when you start bash, unless you're explicitly starting it non-interactively. From the man page:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
If you change .bash_profile, it only applies to new Terminal sessions.
To apply it to an existing session, run source ~/.bash_profile. You can run any Bash script this way - think of executing source as the same as typing commands in the Terminal window (from the specified script).
More info: How to reload .bash_profile from the command line?
Bonus: You can make environment variables available to OSX applications - not just the current Bash session but apps like Visual Studio Code or IntelliJ - using launchctl setenv GOPATH "${GOPATH:-}"
As #kojiro said, you don't want to "run" this file. Source it as he says. It should get "sourced" at startup. Sourcing just means running every line in the file, including the one you want to get run. If you want to make sure a folder is in a certain path environment variable (as it seems you want from one of your comments on another solution), execute
$ echo $PATH
At the command line. If you want to check that your ~/.bash_profile is being sourced, either at startup as it should be, or when you source it manually, enter the following line into your ~/.bash_profile file:
$ echo "Hello I'm running stuff in the ~/.bash_profile!"
No need to start, it would automatically executed while you startup your mac terminal / bash. Whenever you do a change, you may need to restart the terminal.
~ is the default path for .bash_profile
I was getting this error on zsh(mac os Big Sur 11.3), This is how i solved this :-
Go to Terminal.
cd /users/<yourusername>
Once you reach here issue a command :
ls -al
You will see a lot of files and one specific file .zprofile. This is your user profile. We need to edit this.
After this we need to edit the file. Issue the below command :
nano .zprofile
Once you issue this command file will be opened for edit. Add the path details for maven.
M2_PATH="/Users//code/apache-maven-3.8.1/bin" //add your path of maven diretory
PATH="${PATH}:${M2_PATH}"
export PATH
press ctrl + X and save the file.
Issue command after saving the file :
source .zprofile
Once done, you will be able to run the mvn command.
If the problem is that you are not seeing your changes to the file take effect, just open a new terminal window, and it will be "sourced". You will be able to use the proper PATH etc with each subsequent terminal window.
I have installed Hadoop and every time I want to run it, first I have to do this:
source ~/.bash_profile
or it won't recognize the command hadoop
Why is that?
I am on OSX 10.8
Now that we've narrowed down the problem:
Run ps -p $$ at the command line to check if you are, in fact, using a bash shell.
Realize that you are in zsh, which means you should be editing your profile in .zshrc.
Copy the offending lines from .bash_profile to .zshrc, OR
Modify your .zshrc to directly source your .bash_profile.
UPDATE: Do what #TC1 mentions in the comments and keep the shell-specific code in each shell's own profile, and from those profiles, only source shell-agnostic code.
On Mac Catalina, I just had to open "preferences" on terminal and change the "shells open with" from "default" to "Command(complete path)", which the default path was "/bin/zsh". touch ~/.zshrc, if that file doesn't exist already, and copy/paste your stuff from ".bash_profile" into the ".zshrc" file.
To elaborate, with terminal running, I opened "settings" from the Terminal menu on the Mac navbar. On the "General" tab, look for "Shells open with" select "Command (complete path)", and type in /bin/zsh.
bash_profile.sh is applicable for bash shell.
if your default shell is not bash and if your default shell is someother shell for example zsh then you have to manually load the .bash_profile using source ~/.bash_profile.
You can always change the default shell to bash shell so that the .bash_profile file will be automatically loaded.
Inorder to automatically load .bash_profile, you can update your default shell to bash using the command chsh -s /bin/bash
cat /etc/shells will list the default shells available in the
machine
echo $SHELL will display the currently active shell in your machine
To change active shell to a different shell, use chsh -s /bin/bash.
Then echo $SHELL to verify if the shell has changed.
Terminal -> Preference -> profile -> Shell -> Run command : source ~/.bash_profile
Tick on run inside shell.
After doing all those , just logout and check weather everything works fine or not
I tried the approved answer. Changing the .zshrc file works for one of my machines. But for the other one, when I run ps -p $$, it is -sh under the command. And I changed both bash and zsh files, neither of them works for me this time.
So I found this
https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
it mentioned
"When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. "
so I went to that file /etc/profile and add "source ~/.bashrc" in that file. Then it works since every time a terminal is opened, it runs the command in that /etc/profile file.
Not sure if this is the best solution but it works.
sudo nano /etc/bashrc and change that, restarted the terminal and it finally remembered with command. Tried ~/.bash_profile and ~/.bashrc without success, just wasn't sourcing it.
Go to “Preferences/Profiles then look in the right window and find “shell”.
Once in that if your “Startup Run Command” hasn’t been turned on. Click the box to turn it on and in the command section type:
(If you made a .zsh file)
source .zsh ; clear
(If you made a .bash_profile)
source .bash_profile ; clear
Doing this ; clear
Will clear your terminal to a new page so that you don’t see your terminal display:
“Last login: etc
User#user-Mac ~ % source .zsh
If you typed the commands as I said you should just get this:
User#user-Mac ~ %
That way you will be greeted with a clear page with no extra jumbo. Also to make sure that your .zsh or .bash_profile aliases work type the following command to see a list of your custom aliases:
Alias
One alias I like to do is
alias LL=“ls -la”
This will display a tree or the directory you are in as well as hidden files.