119

When I punch from the windows gitbash command line:

set $HOME = c

and do :

echo $HOME

It does not set it to c? How can I change/set the value of an environment variable?

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
bier hier
  • 20,970
  • 42
  • 97
  • 166

3 Answers3

148

A normal variable is set by simply assigning it a value; note that no whitespace is allowed around the =:

HOME=c

An environment variable is a regular variable that has been marked for export to the environment.

export HOME
HOME=c

You can combine the assignment with the export statement.

export HOME=c
chepner
  • 497,756
  • 71
  • 530
  • 681
  • So what does OP's `set` do? – adamdport Apr 16 '18 at 14:20
  • Assuming a "normal" value of `$HOME` (such that the unquoted expansion results in a single word), it sets the first three positional parameters to the values of of `$HOME`, `=`, and `c`, respectively. For example, `set a b c; echo "$2"` would output `b`. – chepner Apr 16 '18 at 15:04
  • 1
    Also true for bash in general. – charles ross Aug 12 '20 at 16:52
  • Worth to note that this variable will NOT be Windows Environment Variables. I mean, opening Windows Settings -> System -> Advanced System Settings -> Environment Variables you cannot find them nor for User neither for System. To achieve this goal use `` setx My_VARIABLE "my value" `` – Alex 75 May 03 '23 at 09:46
63

If you want to set environment variables permanently in Git-Bash, you have two options:

  1. Set a regular Windows environment variable. Git-bash gets all existing Windows environment variables at startup.

  2. Set up environment variables in .bash_profile file.

.bash_profile is by default located in a user home folder, like C:\users\userName\git-home\.bash_profile. You can change the path to the bash home folder by setting HOME Windows environment variable.

.bash_profile file uses the regular Bash syntax and commands

# Export a variable in .bash_profile
export DIR=c:\dir
# Nix path style works too
export DIR=/c/dir

# And don't forget to add quotes if a variable contains whitespaces
export ANOTHER_DIR="c:\some dir"

Read more information about Bash configurations files.

Simeon Leyzerzon
  • 18,658
  • 9
  • 54
  • 82
mosov.a
  • 764
  • 6
  • 7
  • 1
    You may not have answered the OP question but you have certainly helped me out with my question. I was trying to override the PERL5LIB environment variable for Git Bash on Windows and this allowed me to do it easily. – BebopSong Nov 17 '16 at 23:46
  • 1
    You might want to edit #1 to mention it's the same command as in a Windows shell except with different syntax (`setx foo bar` versus `setx foo=bar`). I misread what you were saying with that answer since you didn't mention setx. – Yodle Jul 18 '19 at 20:16
  • For some reason, Git Bash is ignoring my Windows' HOME environment variable. Meaning, it's not using my gitconfig but using its own inside Git/etc. – Unknow0059 Jan 08 '21 at 21:06
  • but you couldn't set HOME or HOMEDRIVE via this method, correct? since you already known .bash_profile is under HOME. – Vincent Jan 01 '23 at 18:05
7

Creating a .bashrc file in your home directory also works. That way you don't have to copy your .bash_profile every time you install a new version of git bash.

kyleus
  • 1,021
  • 12
  • 14