如何编写 bash 脚本来设置全局环境变量? [英] How to write a bash script to set global environment variable?

查看:78
本文介绍了如何编写 bash 脚本来设置全局环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近写了一个设置环境变量的脚本,看看:

Recently I wrote a script which sets an environment variable, take a look:

#!/bin/bash

echo "Pass a path:"
read path
echo $path

defaultPath=/home/$(whoami)/Desktop

if [ -n "$path" ]; then
    export my_var=$path
else
    echo "Path is empty! Exporting default path ..."
    export my_var=$defaultPath
fi

echo "Exported path: $my_var"

它工作得很好,但问题是 my_var 仅在本地可用,我的意思是在我运行脚本的控制台窗口中.

It works just great but the problem is that my_var is available just locally, I mean in console window where I ran the script.

如何编写一个脚本来导出随处可见的全局环境变量?

How to write a script which allow me to export global environment variable which can be seen everywhere?

推荐答案

每个 shell 都有自己的环境.没有通用 环境会神奇地出现在所有控制台窗口中.在一个 shell 中创建的环境变量不能在另一个 shell 中访问.

Each and every shell has its own environment. There's no Universal environment that will magically appear in all console windows. An environment variable created in one shell cannot be accessed in another shell.

它的限制更加严格.如果一个 shell 生成了一个子 shell,则该子 shell 可以访问父 shell 的环境变量,但如果该子 shell 创建了一个环境变量,则无法在父 shell 中访问它.

It's even more restrictive. If one shell spawns a subshell, that subshell has access to the parent's environment variables, but if that subshell creates an environment variable, it's not accessible in the parent shell.

如果您的所有 shell 都需要访问同一组变量,您可以创建一个 startup 文件来为您设置它们.这是通过 $HOME/.bash_profile 文件在 BASH 中完成的(如果 $HOME/.bash_profile 没有,则通过 $HOME/.profile)t 存在)或通过 $HOME/.bashrc.其他 shell 有自己的一组启动文件.一个用于登录,一个用于在没有登录的情况下生成的 shell(和 bash 一样,第三个用于非交互式 shell).请参阅手册页以准确了解使用了哪些启动脚本以及它们的执行顺序).

If all of your shells need access to the same set of variables, you can create a startup file that will set them for you. This is done in BASH via the $HOME/.bash_profile file (or through $HOME/.profile if $HOME/.bash_profile doesn't exist) or through $HOME/.bashrc. Other shells have their own set of startup files. One is used for logins, and one is used for shells spawned without logins (and, as with bash, a third for non-interactive shells). See the manpage to learn exactly what startup scripts are used and what order they're executed).

您可以尝试使用共享内存,但我相信这仅在进程运行时有效,因此即使您找到了设置共享内存的方法,它也会在该命令完成后立即消失.(除了命名管道,我很少使用共享内存).否则,真的没有办法在一个 shell 中设置环境变量并让另一个 shell 自动拾取它.您可以尝试使用 命名管道 或将该环境变量写入文件以供其他 shell 使用.

You can try using shared memory, but I believe that only works while processes are running, so even if you figured out a way to set a piece of shared memory, it would go away as soon as that command is finished. (I've rarely used shared memory except for named pipes). Otherwise, there's really no way to set an environment variable in one shell and have another shell automatically pick it up. You can try using named pipes or writing that environment variable to a file for other shells to pick it up.

想象一下如果有人可以在我不知情的情况下改变一个 shell 的环境可能会发生的问题.

Imagine the problems that could happen if someone could change the environment of one shell without my knowledge.

这篇关于如何编写 bash 脚本来设置全局环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆