简体   繁体   English

我可以在Bash的命令行上设置环境变量吗?

[英]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: 这个命令:

  • invokes bash directly as a login shell ( -l ) 直接调用bash作为登录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 向它传递一个命令( -c ),它定义了感兴趣的环境变量( MSYSTEM=MINGW64 ),然后调用bashexec -l bash )的新副本,它继承了现有的环境,加上新的定义,但是获取了配置文件的来源。 (s)再次,由于-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). (和prepends -$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.

相关问题 如何在不传递命令行参数的情况下在 C++ 中设置我的环境变量 - How can i set my environment variable in c++ without passing command line argument bash在脚本中的命令之前设置环境变量 - bash set environment variable before command in script 为什么在尝试设置环境变量时sh / bash设置命令行参数值? - Why does sh/bash set command line parameter values when trying to set environment variable? 如何在Dockerfile中设置.bash_profile环境变量? - How can I set .bash_profile environment variable in Dockerfile? 如何在命令之前创建一个环境变量前缀的 bash 环境变量? - How can I create a bash environment variable that prefixes an environment variable before a command? 从bash命令行设置php变量 - Set php variable from bash command line 如果在单行中设置了环境变量,bash将为变量分配一个值 - bash assign a value to variable if environment variable is set in a single line 如何在Clink Bash的命令行编辑中将标记设置为Microsoft Windows的cmd.exe - How can I set-mark in Clink Bash's command line editing to Microsoft Windows' cmd.exe Bash:在我可以从命令行访问的 bash 脚本中创建 shell 变量 - Bash: Creating a shell variable in a bash script that I can access from command line 如何从 Heroku 为单个 bash 命令设置环境变量,而不是为整个 shell Z21D9F4440CFB0E51508A 设置环境变量? - How can I set environment variables from Heroku for a single bash command, not for the whole shell session?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM