如何在UNIX中创建一个新的环境变量....? [英] How to create a new environment variable in UNIX....?
问题描述
如何在unix中创建一个新的环境变量并在程序中使用它?
你可以通过 ps -o comm = -p $$
来判断你运行的是什么shell - 我认为这是多或少的普遍性。所以,在bash和某些类似的shell ...
如果要为一个特定的运行创建变量,可以执行
MYVAR = value the_command_that_needs_myvar
如果你想要创建一个完整的shell会话(即直到你注销):
export MYVAR = value
...然后你可以运行:
the_command_that_needs_myvar
...在该会话期间您喜欢的次数,并且仍然会看到 MYVAR
的值为值
。
如果您希望为自己设置所有登录会话,请将其放在〜/ .profile
中。
请注意,bash的初始化文件可以是一个伟大的大型WTF。取决于它是否以交互方式运行,通过网络运行,在本地运行,并且取决于它是否被调用为 sh
或 bash
,它将选择性地阅读〜/ .bashrc
,〜/ .profile
和〜/ .bash_profile中
。有关详细信息,请阅读 bash
手册页的FILES部分。
如果您希望为每个用户设置它每次登录时,都将其放入文件 / etc / profile
(尽管还有 / etc / environment
,我不知道这是多么广泛使用。)。
查看问题还有一些更多的细节。 p> (请注意,有些建议会根据您或其他用户使用bash,dash,csh,ksh等而有所不同,但它应该适用于大多数用例)。 How to create a new environment variable in unix and use it in a program?????? You can tell what shell you're running by If you want to create the variable for one specific run, you can do If you want to create it for an entire shell session (ie. until you log out): ...and then you can run: ...as many times as you like during that session, and it will still see If you want it to be set for yourself, for all your login sessions, put it in Please note that bash's initialisation files can be one great big WTF. Depending on whether it is run interactively, over a network, locally, AND depending on whether it is invoked as If you want it to be set for every user, every time they log in, put it in the file Check out the question "How to set environment variable for everyone under my linux system?" for some more details, too. (Beware, some of this advice will vary depending on if you, or other users, use bash, dash, csh, ksh, etc... but it should work for most use cases.) 这篇关于如何在UNIX中创建一个新的环境变量....?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
ps -o comm= -p $$
— I think that's more-or-less universal. So, in bash and certain similar shells...MYVAR=value the_command_that_needs_myvar
export MYVAR=value
the_command_that_needs_myvar
MYVAR
as having the value value
.~/.profile
.sh
or bash
, it will selectively read some combination of ~/.bashrc
, ~/.profile
and ~/.bash_profile
. Read the FILES section of the bash
man page for details./etc/profile
(although there's also /etc/environment
, I'm not sure how widely used that is.).