[英]Can I set an environment variable on Bash's command line?
I am trying to set an environment variable for Bash. 我正在尝试为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. 但是,我需要在任何shell的启动脚本(包括
/etc/profile
)之前设置它,因为/etc/profile
根据此变量的值采取不同的行为。
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. 具体来说,我想创建一个像
git-bash
一样的MinTTy快捷方式,但是我需要在shell启动之前设置MSYSTEM
环境变量,或者至少在它开始处理任何启动脚本之前。
A solution that has MinTTy setting the environment variable before it starts the shell will also be accepted. MinTTy在启动shell之前设置环境变量的解决方案也将被接受。
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. 编辑:我真正想要的是BASH的一个命令行选项,它将设置一个环境变量,有点类似于大多数C(和其他)编译器的
-D
选项。 This would be a "general case" solution. 这将是一个“一般情况”的解决方案。 Alternatively, a similar option (command line or configuration) to MinTTy will also do the job.
或者,MinTTy的类似选项(命令行或配置)也可以完成这项工作。
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 exec
s another shell as a login shell. 根据我的具体需要,我有一个潜在的解决方法:运行一个BASH脚本 - 没有启动脚本 - 设置我所需的变量,并
exec
另一个shell作为登录shell。
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: 这个命令:
bash
directly as a login shell ( -l
) bash
作为登录shell( -l
) -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
-c
),它定义了感兴趣的环境变量( MSYSTEM=MINGW64
),然后调用bash
( exec -l bash
)的新副本,它继承了现有的环境,加上新的定义,但是获取了配置文件的来源。 (s)再次,由于-l
-
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). -
以$0
( -bash
)中报告的可执行文件名称,如果你只使用-
启动Mintty会发生这种情况,这就是常规的Cygwin64 Terminal
快捷方式所做的Cygwin64 Terminal
)。 An alternative is to set the environment variable in Windows first. 另一种方法是首先在Windows中设置环境变量。
[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. [不是OP的选项] 如果环境变量应始终具有相同的值,请按如下所示永久设置:运行
sysdm.cpl
,转到“ Advanced
选项卡,单击“ Environment Variables...
并根据需要定义变量MSYSTEM
。
To define the variable ad-hoc , create a batch file as follows and make the shortcut target that batch file: 要定义变量ad-hoc ,请按如下所示创建批处理文件,并使批处理文件的快捷方式目标:
@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. 注意:在打开Mintty之前,从快捷方式打开批处理文件会短暂打开常规控制台窗口,这可能是不受欢迎的。
A simple helper WSH script, as demonstrated in this answer of mine, can prevent this. 一个简单的帮助WSH脚本,如我的这个答案所示 ,可以防止这种情况。
You should just be able to do the same as you do in command prompt. 您应该能够像在命令提示符中那样执行相同操作。 Therefore, you can do:
因此,您可以这样做:
set VAR=VarContents
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. 虽然我已经接受了上面的答案,但我发现这个链接专门解决了我的问题的第二部分(特定于Mintty)或者在运行命令之前设置环境变量的另一种方法。
The contents of the Windows shortcut can be: Windows快捷方式的内容可以是:
C:\cygwin64\bin\mintty.exe -t "Title" /bin/env "MSYSTEM=MINGW64" /bin/bash -l
(Suggested by Mintty Tips : Setting environment variables .) ( Mintty Tips建议 : 设置环境变量 。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.