当shell脚本是从桌面快捷方式调用的.bashrc不读 [英] .bashrc not read when shell script is invoked from desktop shortcut

查看:261
本文介绍了当shell脚本是从桌面快捷方式调用的.bashrc不读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题,了解在Linux中的行为。总之,在Linux上,如果我调用我的sh脚本从桌面快捷方式,那么脚本无法看到最新的环境变量(以bashrc中集)。所以我想知道,在什么范围这个shell脚本位于何处?

要创建测试用例和复制:


  1. 创建一个简单的shell脚本'testme.sh


    !/ bin / sh的
    回声这是一个测试脚本检查的环境变量;
    回声的testvar = $的testvar;
    阅读
    回声完成;


  2. 创建上面的脚本的桌面快捷方式。


    CD〜/桌面
    六mytest的-desktop.desktop
    //为mytest的-desktop.desktop内容是:
    [桌面入口]
    版本= 1.0
    类型=应用
    NAME = TestAbhishek
    EXEC = /家庭/阿布舍克/测试/ hello.sh
    终端=真


  3. 现在,更新您的.bashrc文件中设置变量

   


   出口的testvar = test_this_variable
   


  1. 打开一个全新的终端,并使用它像'〜/ testme.sh'//此可以看到从.bashrc文件变量的testvar'的值完整路径执行脚本。


  2. 现在,只需双击并执行桌面快捷方式。
    //这应该打开一个终端和'的testvar空白打印出值。
    //所以我的问题是,谁是针对终端的家长通过此快捷方式打开?


我已经试过这对RHL。
即时寻找这个问题的解决方案或W / A,希望有人能帮助尽快

谢谢,
阿布舍克。


解决方案

看到 INVOCATION 在bash手册页的部分。下面是摘录


  

当bash是作为交互
  登录shell,或者作为非交互式
  与--login选项外壳,它
  第一读出并执行的命令
  从文件/ etc / profile文件,如果
  文件已存在。读取文件之后,
  它查找〜/ .bash_profile中,
  〜/ .bash_login文件,和〜/ .profile中,在那
  顺序,并且读取和执行命令
  从存在并且是第一位的
  可读。该--noprofile选项可能
  当壳开始被使用的
  抑制这种行为。


  
  

当一个登录shell退出时,bash读取
  并执行从文件的命令
  〜/ .bash_logout中,如果存在的话。


  
  

当一个交互的shell,是不是
  登录shell启动时,bash读取
  和〜/ .bashrc中执行命令,
  该文件是否存在。
这可能是
  通过使用--norc选项抑制。
  该文件--rcfile选项将迫使
  庆典读取和执行命令
  从文件而不是〜/ .bashrc中。


长话短说,如果你要非交互的shell的有一定的ENV瓦尔设置,然后把它们放在〜/ .bash_profile中而不是在〜/ .bashrc

I have a simple problem understanding a behavior in linux. In short, on linux if i invoke my sh script from a 'Desktop Shortcut' then the script cannot see the latest environment variables (set in bashrc). So i was wondering that in what scope is this shell script located ?

To create a testcase and reproduce:

  1. Create a simple shell script 'testme.sh' :

    !/bin/sh
    echo "Hi This is a test script checking the env var";
    echo "TESTVAR = $TESTVAR";
    read in
    echo "Done";
    

  2. create a desktop shortcut for the script above.

     
    cd ~/Desktop
    vi mytest-desktop.desktop 
    
    
    //Contents for mytest-desktop.desktop are : 
    [Desktop Entry]
    Version=1.0
    Type=Application
    Name=TestAbhishek
    Exec=/home/abhishek/test/hello.sh
    Terminal=true
    

  3. Now update your .bashrc file to set the variable

   export TESTVAR=test_this_variable
   

  1. Open a brand new terminal and execute the script using it's complete path like '~/testme.sh' //This can see the value for variable 'TESTVAR' from the .bashrc file.

  2. Now, simply double click and execute the Desktop shortcut. //This should open a terminal and print out value for 'TESTVAR' as blank. //So my question is, who is the parent for the terminal opened by this shortcut?

I've tried this on RHL. Im looking for a solution or a w/a for this problem, hope someone can help soon.

Thanks, Abhishek.

解决方案

See the INVOCATION section of the bash manpage. Here is an excerpt

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.

Long story short, if you want non-interactive shell's to have certain ENV vars set, then put them in ~/.bash_profile instead of ~/.bashrc

这篇关于当shell脚本是从桌面快捷方式调用的.bashrc不读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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