“Environment variable change in shell but won’t export” 变量在 shell 中更改但不导出。

huangapple go评论55阅读模式
英文:

Environment variable change in shell but won't export

问题

我在设置永久环境变量时遇到问题。例如:

HISTSIZE=0
export HISTSIZE
echo $HISTSIZE

这个变量会在当前shell中改变。但是如果我打开另一个标签页,或者关闭然后重新打开shell,该变量会恢复到其原始值1000。

我还尝试过在~/.bash_profile中编写一个脚本来源化这个变量。但是它仍然存在同样的问题,即该变量只在特定的shell中起作用。如何创建永久性的变化呢?

英文:

I am having issues with setting permanent environment variables in my shell. For example

HISTSIZE=0
export HISTSIZE
echo $HISTSIZE

The variable will change in the shell. But if I open another tab or close and reopen the shell the variable reverts to its original value of 1000.

I have also tried sourcing the variable with a script written in ~/.bash_profile. But it leaves the same issue of the variable only working in that specific shell. How can I create a permanent change?

答案1

得分: 4

如果您打开一个新的标签页,新shell的父进程不是当前的shell,而是您的终端模拟器,因此导出HISTSIZE不会影响新shell的环境。

由于HISTSIZE仅由shell本身使用,根本不需要导出它。在.bashrc中设置其值,以便任何新的交互式shell都会获得初始化的值。

HISTSIZE=0

如果您的终端模拟器配置为启动登录shell(在macOS上很常见,我认为在Linux上不太常见),则不会使用.bashrc。在这种情况下,我建议在您的.bash_profile的末尾添加. .bashrc,以便交互式登录shell初始化与交互式非登录shell相同。

英文:

If you open a new tab, the parent process of the new shell isn't your current shell, but your terminal emulator, so exporting HISTSIZE doesn't affect the environment of the new shell.

Since HISTSIZE is only used by the shell itself, there's no need to export it at all. Set its value in .bashrc so that any new interactive shell gets the value initialized.

HISTSIZE=0

If your terminal emulator is configured to start a login shell (common on macOS, I assume much less so in Linux), .bashrc won't be used. In such a case, I recommended adding . .bashrc to the very end of your .bash_profile, so that an interactive login shell is initialized the same as an interactive non-login shell.

答案2

得分: 2

有些终端模拟器不会将新标签页作为登录shell运行。例如,在Gnome终端中,您应该执行以下步骤:

  1. 转到编辑 -> 配置文件首选项。
  2. 选择“标题和命令”选项卡。注意“将命令作为登录shell运行”复选框未被选中!请选中它。

此外,在shell会话中设置变量不会使其在以后的会话中保持永久性。例如,导出一个变量将使它在从当前会话创建的任何后续进程中可用。

要使其在某种程度上保持永久性,您必须将其添加到例如“.bashrc”文件中。

请注意

> 建议使用shell配置文件,例如~/.bashrc,~/.bash_profile和~/.bash_login来设置环境变量。尽管这对于从shell启动的程序可能有效,但在桌面会话中从图形环境启动的程序默认情况下无法使用这些文件中设置的变量。

引自Ubuntu帮助

因此,要决定将其添加到哪里,请阅读详细手册

英文:

Some terminal emulator doesn't run new tabs as login shell. E.g. in Gnome Terminal You should:

  1. List item
  2. Go to Edit -> Profile Preferences.
  3. Select the Title and Command tab. Notice how the Run command as login shell checkbox is unchecked! Check it.

Furthermore setting a variable in a shell session does not make it permanent for later sessions. E.g. exporting a variable makes it available for any further processes that's got created from the actual session.

To make it somewhat permanent You have to add it to e.g. .bashrc

Do note:

> Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.

Quoted from the Ubuntu help.

So to decide where to add it please read the fine manual

答案3

得分: 0

查看bash的手册页面:.bash_profile 仅在交互式登录shell中读取。如果是交互式非登录shell,则会处理 .bashrc

我建议将那些应在每个交互式shell中执行的设置放入一个单独的文件中(例如:~/.bash_interactive),然后从 .bash_profile.bashrc 中引用该文件。

英文:

Look at the bash man page: .bash_profile is only read for interactive login shells. If it is an interactive non-login shell, .bashrc is processed instead.

I suggest putting those settings which should be performed in every interactive shell, into a separate file (say: ~/.bash_interactive) and source this file from .bash_profile and .bashrc.

huangapple
  • 本文由 发表于 2020年1月6日 21:42:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定