gpt4 book ai didi

linux - 从桌面快捷方式调用 shell 脚本时未读取 .bashrc

转载 作者:IT王子 更新时间:2023-10-29 00:25:34 24 4
gpt4 key购买 nike

我在理解 Linux 中的行为时遇到了一个简单的问题。简而言之,在 Linux 上,如果我从“桌面快捷方式”调用我的 sh 脚本,则该脚本无法看到最新的环境变量(在 bashrc 中设置)。所以我想知道这个 shell 脚本位于什么范围内?

创建测试用例并重现:

  1. 创建一个简单的 shell 脚本“testme.sh”:

    !/bin/shecho "Hi This is a test script checking the env var";echo "TESTVAR = $TESTVAR";read inecho "Done";
  2. 为上面的脚本创建一个桌面快捷方式。

     cd ~/Desktopvi mytest-desktop.desktop //Contents for mytest-desktop.desktop are : [Desktop Entry]Version=1.0Type=ApplicationName=TestAbhishekExec=/home/abhishek/test/hello.shTerminal=true
  3. 现在更新您的 .bashrc 文件以设置变量
   export TESTVAR=test_this_variable   
  1. 打开一个全新的终端并使用它的完整路径(如“~/testme.sh”)执行脚本//这可以从 .bashrc 文件中看到变量“TESTVAR”的值。

  2. 现在,只需双击并执行桌面快捷方式。//这应该打开一个终端并将“TESTVAR”的值打印为空白。//所以我的问题是,这个快捷方式打开的终端的父级是谁?

我已经在 RHL 上试过了。我正在为这个问题寻找解决方案或 w/a,希望有人能尽快提供帮助。

谢谢,阿比舍克。

最佳答案

请参阅 bash 联机帮助页的 INVOCATION 部分。这是摘录

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

长话短说,如果您希望非交互式 shell 设置某些 ENV 变量,则将它们放在 ~/.bash_profile 而不是 ~/.bashrc

关于linux - 从桌面快捷方式调用 shell 脚本时未读取 .bashrc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4477328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com