47

I use MinGW on Windows 7. I have a .bashrc with some aliases in it. The file is in my home folder which is where MinGW starts me in, so it also believes that the folder is my home folder. It does not load the contents of the folder automatically. I have to run the bash command to get the aliases to work.

I have tried renaming it to .bash_profile. This only made things worse as it didn't load automatically and also didn't load when I ran bash manually.

How can I fix this problem?

1
  • Sorry the path should have been: C:\MinGW\msys\1.0\home\Your_Username_Here\.profile
    – user125075
    Mar 27, 2012 at 18:15

7 Answers 7

62

bash is probably getting started as a login shell, in which case it doesn't read .bashrc automatically. Instead, it reads .bash_profile. From the Bash manual:

So, typically, your `~/.bash_profile' contains the line

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

after (or before) any login-specific initializations.

So in summary, create a .bash_profile file in your homedir, and add the line quoted above.

If your bash is actually getting invoked as sh, then you'll need to use .profile instead of .bash_profile (see the "Invoked with name sh" section of the Bash manual link above).

5
  • 1
    Thanks for the idea, didn't work though. Like I said in the question, putting the alias commands in .bash_profile doesn't work at all, so it seems like that doesn't get read.
    – KurToMe
    Mar 27, 2012 at 17:15
  • 6
    This gave me the idea to do exactly what you said except to put it in the .profile file instead. For some reason that worked (maybe MinGW doesn't use Bash by default or something crazy). If you add this option to your answer I'll mark it as accepted.
    – KurToMe
    Mar 27, 2012 at 17:17
  • 1
    Ah, there was a typo in my question that I fixed, I was calling it .bash_rc in the question, but I meant .bash_profile.
    – KurToMe
    Mar 27, 2012 at 17:41
  • 1
    somewhat more terse: [ -f ~/.bashrc ] && . ~/.bashrc Mar 27, 2012 at 18:42
  • 1
    @KurToMe I edited to cover your situation.
    – jjlin
    Mar 27, 2012 at 19:04
10

I am running Windows XP and had the same problem. I found HOWTO Create an MSYS Build Environment.

This is the important line:

To help identify the runtime build and the current working directory, the following can be added to the ~/.profile file.

In MinGW shell, I created .profile:

cd ~
touch .profile

I used Notepad++ to edit it as a Unix format text file named .profile and saved it in my home directory, C:\MinGW\msys\1.0\home\Your_Username_Here\.profile

Then I added my alias and saved:

alias n='nano -w'

Then I fired up the MinGW Shell shortcut from my start menu and hurray, it worked! nano with no text wrapping.

I hope this helps you.

1
  • What you're saying doesn't jive ... ~ means the users home directory ... then your stating c:\MinGW\msys\1.0\home ... that doesn't make any sense the tilde is a reference to the USERS home ... on windows that %userprofile% ... Oct 20, 2012 at 5:09
3

For me for MINGW installed with GIT, worked: .bash_profile put in C:\Users\[user_name]

This is also the directory where ~ points to in shell (pwd).

Just like that :)

3

I did not find the .bash_profile to work for me (it wasn't being read), so I took the .profile approach and put within it:

exec bash

This replaces my current shell with a fresh start of bash, which read my .bashrc

I'm thinking that using a .profile suggests that sh is used at login, not bash.

2

Along the lines of @Koshmarr, but slightly different. My mingw64, downloaded from git-scm.com, would load /c/Users/[user-name] on startup. Yet my home drive (found by cd ~) was in /h/. I put a .bashrc and .bash_profile into my ~ drive and everything worked.

In .bash_profile:

    source ~/.bashrc;

Just figure out where MINGW considers home and put the .bash_profile there.

1
  • Indeed, this was the case, as an app started MINGW64 in a funny way, dumping me into a different directory. Only way out was to do cd ~; pwd and put your .bashrc in there.
    – not2qubit
    Jun 10, 2023 at 21:23
1

How to set up MSYS Bash initialization files

In the Windows OS, the way you point to the initialization files used by Bash (i.e., profile, bash_profile, bashrc) is different compared to the Linux OS.

The difference between the Windows and Linux OS is their file system structure, which leads to difference in Bash file locations and file naming conventions.

How to set up MSYS Bash initialization files (i.e., profile, bash_profile, and bashrc)

1) In WINOS, open the 'etc' folder located here 'C:\msys\1.0\etc\'. Note. you should see a file 'profile' inside 'etc' folder.

2) Save a no-file-extension file (i.e., when you save set the save as type to '.all files') with the file name '.bash_profile' to 'C:\msys\1.0\home\USERNAME'. Note. there is dot as prefix for the file name (e.g. '.bash_profile') as you would expect in Linux OS Bash setup

2) Save a no-file-extension file (i.e., when you save set the save as type to '.all files') with the file name '.bashrc' to 'C:\msys\1.0\home\USERNAME'. Note. there is dot as prefix for the file name (e.g. '.bash_profile') as you would expect in Linux OS Bash setup

4) Open the 'profile' file in a text editor (sublime text x is good) Note: The file location of 'profile' is 'C:\msys\1.0\etc\'

Inside 'profile' file, scroll to line of text at the end of the file (i.e., scroll to the bottom) you should see a line a test indicating Bash to change its current directory to HOME (In MSYS, HOME is as defined in 'profile' file)

CD $HOME

5) Before the line with text 'CD $HOME' copy the following code -- adding this script within 'profile' will tell MSYS bash to run the bash_profile #--------------------------------------------------------------------------- #modified BEGIN

# Note. -f is a flag in the file-test operator set of bash commands
#       that test whether file exists
echo '[i] INFO loading personal environment variables and startup   programs.... '
BASHRC="$HOME/.bash_profile"
if [ -f "$BASHRC" ]; then
  source "$BASHRC"
fi

#modified END
#---------------------------------------------------------------------------

6) Open the 'bash_profile' file in a text editor (Sublime Text x is good) and the end of the file (at bottom of file)

7) Copy the following code at end of file -- adding this script within '.bash_profile' will tell Bash to run the '.bashrc'

#---------------------------------------------------------------------------
#modified BEGIN

# Note. -f is a flag in the file-test operator set of bash commands
#       that test whether file exists
BASHRC="$HOME/.bashrc"
if [ -f "$BASHRC" ]; then
  source "$BASHRC"
fi

#modified END

#---------------------------------------------------------------------------

8) O.K., now let's write a small script inside '.bashrc' to see if 'profile' is calling 'bash_profile' and if 'bash_profile' is calling '.bashrc'.

8.1) Think of a command name you wish to create. Let's use 't' to mean test

8.2) Open Bash terminal and type 't'. Bash should reply 't: command not found' -- if it does not say 't: command not found' then repeat 8.1 and 8.2 until your command name says 'command not found'. You do not want to alias a command name that is assigned to bash BAD!, so it must be 'command not found'

8.3) O.K., 't' gave me a 't: command not found'. Now let's write the script into '.bashrc'

8.4) In '.bashrc' write

# Use double quotes or bash will not write it
alias t="echo passed test"

8.5) Save file '.bashrc' in 'C:\msys\1.0\home\USERNAME'

9) Almost there. Now restart the Bash terminal, and make sure to close any previously opened Bash terminals

10) Type in terminal 't'. It should reply 'passed test'. If not, carefully check that code was correctly written (start by checking quotes)

Some notes 1) 'source filename' is the same as '. filename'

2) " " double quotes allow variables to be replaced by its contents single quotes do not

3) $HOME is /home/USERNAME same as ~ is /home/USERNAME

4) A function are always preferred over alias 2

For more detail see BASH reference

I am new to Bash. So there is no guarantee I did this the best way.

Good luck!

1
  • I have no idea where you got the 'C:\msys\1.0' directory that has your etc directory within it. Nov 25, 2022 at 14:13
0

I have a full setup guide in my answer here: Installing & setting up MSYS2 from scratch, including adding all 7 profiles to Windows Terminal.

Here's the gist of how I handled the problem of getting my MSYS2 Bash terminals to source my ~/.bashrc file. For full details, see my answer above:

  1. Change your HOME (~) dir from MSYS2's default of C:\msys64\home\my_username to your regular Windows home dir of C:\Users\my_username:

    Open up C:\msys64\home\my_username\.bash_profile in a text editor, such as VSCode. Modify it so that it looks like this. Note: you can just comment everything out with # and add this to the top:

    # Change your home (`~`) dir to `C:\Users\my_username`
    HOME="/c/Users/$(whoami)"
    
    # Source your `C:\Users\my_username\.profile` file, if it exists
    if [ -f "$HOME/.profile" ]; then
      . "$HOME/.profile"
    fi
    

    Close and re-open Windows Terminal. Run echo ~ and echo "$HOME" and ensure they now both show /c/Users/my_username. Run ls -a and ensure you see the contents of your normal Windows home directory for your user. pwd should show that you are currently in that directory too,

Notice the lines above where I source the ~/.profile file via . "$HOME/.profile", if it exists.

Now, create a ~/.profile file, and put this into it. This will source your ~/.bashrc file, if it exists:

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

Now, create a ~/.bashrc file. Open any of your seven MSYS2 environment terminals, now, and you should see that your ~/.bashrc file is automatically sourced. The process is:

  1. C:\msys64\home\my_username\.bash_profile gets sourced automatically by your MSYS2 Bash terminal. That .bash_profile file then sets your HOME dir, per our code above, and sources ~/.profile.
  2. ~/.profile sources ~/.bashrc.

Voila! Whenever you need to re-source everything, if you make a change or something, just run . ~/.profile in your MSYS2 Bash terminal. Or, running just . ~/.bashrc will re-source just the ~/.bashrc file.

See also

  1. If you don't know what "sourcing" means exactly, see my answer here: source (.) vs export (and also some file lock [flock] stuff at the end)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .