34

I have some aliased defined on my .bashrc that I'd like to use on my Intellij IDEA's terminal. Why is .bashrc not sourced?

Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90
  • Useful to understand why: [Why doesn't .bashrc run automatically?](https://apple.stackexchange.com/a/13019/116146) and [What is the difference between .bash_profile and .bashrc?](https://apple.stackexchange.com/questions/51036/what-is-the-difference-between-bash-profile-and-bashrc) – Ricardo Mar 19 '21 at 00:49
  • My issue was with launching from the doc as a GUI for Android Studio. I updated my system-wide path from this answer: https://stackoverflow.com/a/3756686/1451443 – notbad.jpeg May 01 '22 at 20:18

15 Answers15

53

.bashrc is only loaded when Bash is running in "interactive" mode. For some reason, IntelliJ defaults to running Bash in non-interactive mode, but this is easy to change. Open the IntelliJ Settings window, then open "Tools -> Terminal", and add -i to the Shell path.

(Note that in this screenshot, I have also changed the default shell, because I'm on a Mac, which makes it difficult to update /bin/bash. Unless you have installed a different version of Bash, do not blindly copy this change!)

enter image description here

Ricardo
  • 3,696
  • 5
  • 36
  • 50
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
  • This is best solution and this must be marked as best answer! – 0xDEADBEEF Oct 08 '20 at 07:52
  • 3
    So if your `Terminal > Shell path` setting calls `bash -i` (interactive), it will run `~/.bashrc` on opening a new tab; if it calls `bash` (non-interactive), it will run `~/.bash_profile`. *Checked against WebStorm 2020.3.3 on macOS Catalina.* – Ricardo Mar 19 '21 at 00:42
  • This solution does not work on my windows git bash environment. Instead what did work was this solution provided: https://intellij-support.jetbrains.com/hc/en-us/community/posts/205437150/comments/204862230 – Barney Feb 07 '22 at 20:54
  • 1
    @Barney Interesting... `--login -i` should also work, but `login` may not be what you want. I don't know why `-i` on its own wouldn't fix the issue for you, though. – Kyle Strand Feb 09 '22 at 19:18
  • 1
    @KyleStrand oh `--login` or `-l` alone (without `-i`) does what I need which is to source `.bash_profile`. This SO question is about `.bashrc` so I now realize this is not apples to oranges. Thank you anyway and feel free to disregard! – Barney Feb 10 '22 at 06:24
15

Had the same issue with IntelliJ. Solved this by setting the value /bin/bash --login in setting->Tools->Terminal->Shell path.

/bin/bash --login forces the bash to read the ~/.bash_profile.

https://intellij-support.jetbrains.com/hc/en-us/community/posts/205437150-Terminal-not-sourcing-bash-profile?page=1#community_comment_360000167679

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
Ricardo 2040
  • 151
  • 1
  • 3
11

In your home directory, add theses lines to .profile (create the file if it does not exist), in order to source .bashrc:

if [ "$SHELL" = "/bin/bash" ]; then
    . ~/.bashrc
fi   
Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90
SLePort
  • 15,211
  • 3
  • 34
  • 44
10

For me, changing the line

Exec="/opt/idea-IU-183.4284.148/bin/idea.sh" %f

to

Exec=bash -ic "/opt/idea-IU-183.4284.148/bin/idea.sh" %f 

worked. We exploit the hack that the interactive shell loads .bashrc :)

Saravanabalagi Ramachandran
  • 8,551
  • 11
  • 53
  • 102
  • While the solution is highly installation-specific, I've found it useful in another case. I use node.js with `nvm` and for node to be available in `PATH`, `.bashrc` should be executed before IDE. I've improved the solution like this: `bash -ic "exec /path/to/webstorm.sh %f"` That is, handle `%f` and use `exec` to replace bash. – Konstantin Pelepelin Jul 06 '20 at 15:27
  • This solution works for me with Ubuntu 20.04. Thanks! – jyu Aug 27 '20 at 02:28
  • Also works for me on Kubuntu 20.04. NPM installed via NPM was not available during maven builds delegating to shell exec. – Marius Schmidt Jan 24 '21 at 21:50
  • This worked atleast on ubuntu 20.04.5 LTS. If some one failing to find path where to replace. Here it is ~/.local/share/applications/jetbrains-rider.desktop or intellj or webstorm – Azerue Nov 16 '22 at 09:06
9

On Mac OSX Catalina, "/bin/bash" and ~/.zprofile worked for me:

enter image description here

Update:

Apple has changed the default shell to zsh. Therefore you have to rename your configuration files. .bashrc is now .zshrc and .bash_profile is now .zprofile.

masoud soroush
  • 677
  • 11
  • 21
3

My settings are in ~/.bash_profile . I solved it using:

echo "source ~/.bash_profile" >> .profile
Eidan Spiegel
  • 307
  • 1
  • 2
  • 10
2
sudo echo ". ~/.bashrc" >> /etc/bash.bashrc
Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90
Ognjen
  • 61
  • 3
  • 1
    @AlexanderSuraphel As I understood, idea loads only default bash profile. With adding source of your home .bashrc at the end of it, it will be loaded by default. btw, first answer doesn't work for me. – Ognjen Sep 19 '16 at 13:19
  • 7
    Even though this might work, I wouldn't do this. I quite often make changes to my .bashrc -file. The .bashrc -file contains configuration for an individual user whereas /etc/bash.bashrc is used for every user using the system. The command suggested simply adds my entire configuration to the system configuration which will then apply it to every user using the system. Apparently Intellij IDEA picks up the system-file, but not the file intended for the individual user. – Matti Nov 30 '17 at 06:40
1

Because > operator doesn't pass the sudo permissions to the write process.

I did this :

echo ". ~/.bashrc" | sudo tee /etc/bash.bashrc
Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
1

I noticed that .bashrc isn't sourced only when I first install IntelliJ and run it directly via the idea.sh script in bin/.

Once I create the desktop entry via Tools -> Create Desktop Entry... and start it from the Ubuntu dash, .bashrc is sourced properly.

Zoltán
  • 21,321
  • 14
  • 93
  • 134
1

If you have recently moved to zsh from bash then go to ~/.zshrc file and update $PATH variable there:

Default value in .zshrc:

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

Change to same PATH variable set in .bashrc and uncomment it

# If you come from bash you might have to change your $PATH.
export PATH=$HOME/bin:/usr/local/bin:<my-bash-paths>:$PATH
Aman Gupta
  • 3,627
  • 4
  • 40
  • 64
0

None of these answers worked for me. What did work is

sudo nano /etc/environment

...then manually adding my export and alias commands here to make them system wide.

But be careful, do NOT mess up the PATH or you'll have trouble logging back in to your desktop environment, or many other issues.

TetraDev
  • 16,074
  • 6
  • 60
  • 61
0

I had this problem because the default shell had been changed to zsh.

I echoed the shell name with echo $SHELL to see this (thanks How to get default shell).

Then I changed it back to /bin/bash with this command: chsh -s /bin/bash (thanks https://apple.stackexchange.com/a/100476/176809).

Ben
  • 54,723
  • 49
  • 178
  • 224
0

Create a bash script with the content;

echo "source ~/.bash_profile" >> .profile

Then in Intellij go to preferences/tools/Startup Tasks;

create a run configuration that runs your bash script and you're good to go.

0

I just restarted my MAC and it picked up the new stuff.

Ashok J
  • 3
  • 2
0

For me at least, the only thing that works was to put this in the Shell path variable inside of Tools > Terminal :

/bin/bash --rcfile ~/.bashrc

Juan Antonio
  • 2,451
  • 3
  • 24
  • 34
  • Sorry for the abuse of system here. Congrats on being my "Strunk and White" twin -- we both received this badge around the same time and have similar overall points. And this despite your English! I also like the "unsung hero" badge - don't have one myself, but a nice concept. – Josiah Yoder Dec 02 '22 at 19:25