4

I am trying to set an environment variable for Bash. However, I need this to be set before any of the shell's startup scripts (including /etc/profile), because /etc/profile acts differently based on the value of this variable.

Specifically, I want to create a shortcut to MinTTy that works like git-bash, but I need to set the MSYSTEM environment variable before the shell starts, or at least before it starts processing any startup scripts.

A solution that has MinTTy setting the environment variable before it starts the shell will also be accepted.

Edit: What I am really looking for is sort of a command-line option to BASH that will set an environment variable, somewhat akin to the -D option to most C (and other) compilers. This would be a "general case" solution. Alternatively, a similar option (command line or configuration) to MinTTy will also do the job.

For my specific need, I have an idea for a potential work-around: Run a BASH script - with no startup scripts - that sets my required variable and execs another shell as a login shell.

Menachem
  • 911
  • 7
  • 22
  • not sure if this will work for your case, but trycreating a wrapper script that set the env variable and then execute =>>"source /etc/profile". plus any other scripts that get executed at bootup/logintim. – z atef Apr 21 '17 at 18:34
  • @zee I considered that. But then I have to fully replicate the entire startup process: login/interactive, the correct `profile` and `bashrc` files, etc. – Menachem Apr 21 '17 at 18:46
  • I can try starting the shell with no startup files, but execute only a shell script that sets the environment variable and then `exec`s a login or interactive shell. – Menachem Apr 21 '17 at 18:48
  • Why are you worried about the startup processes? Would they over write your env variables ? Or is just a preference? – z atef Apr 22 '17 at 00:19

3 Answers3

2

Define the target of your shortcut file as follows:

C:\cygwin64\bin\mintty.exe /bin/bash -l -c "MSYSTEM=MINGW64 exec -l bash"

This command:

  • invokes bash directly as a login shell (-l)
  • passes it a command (-c) that defines the environment variable of interest (MSYSTEM=MINGW64) and then invokes a new copy of bash (exec -l bash), which inherits the existing environment, plus the new definition, but sources the profile(s) again, due to -l
    (and prepends - to the executable name reported in $0 (-bash), as would happen if you started Mintty with just -, which is what the regular Cygwin64 Terminal shortcut does).

An alternative is to set the environment variable in Windows first.

  • [Not an option for the OP] If the environment variable should always have the same value, set it persistently as follows: run sysdm.cpl, go to the Advanced tab, click on Environment Variables... and define variable MSYSTEM as needed.

  • To define the variable ad-hoc, create a batch file as follows and make the shortcut target that batch file:

    @echo off
    
    # Define the env. variable with the desired value.
    set "MSYSTEM=MINGW64"
    
    # Invoke Mintty with a login shell, which will now see the env. variable.
    # Adjust the path to mintty.exe as needed.
    c:\cygwin64\bin\mintty.exe -
    

Note: Opening the batch file from a shortcut briefly opens a regular console window before opening Mintty, which may be undesired.

A simple helper WSH script, as demonstrated in this answer of mine, can prevent this.

Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    @Menachem: My pleasure; good point re `-`; I've updated the answer, though, to be consistent with the default behavior, I've used `exec -l bash`, so that `$0` reports `-bash`, not `-/bin/bash`. – mklement0 Apr 24 '17 at 21:28
2

You should just be able to do the same as you do in command prompt. Therefore, you can do:

set VAR=VarContents
J03L
  • 314
  • 3
  • 17
  • Can I suggest you delete this answer? It duplicates content in my answer (which was there even in the original form of my answer at the time you posted yours), and by itself it is not enough to solve the OP's problem. – mklement0 Apr 24 '17 at 21:33
1

Although I already accepted an answer above, I found this link that specifically addresses the second part of my question (Mintty specific) or an alternative way of setting an environment variable before running a command.

The contents of the Windows shortcut can be:

C:\cygwin64\bin\mintty.exe -t "Title" /bin/env "MSYSTEM=MINGW64" /bin/bash -l

(Suggested by Mintty Tips:Setting environment variables.)

Menachem
  • 911
  • 7
  • 22