ssh 远程登录命令读取不到环境变量

1、ssh user@remote 'echo $app_path && test.sh'

test.sh:

   echo $app_path

2、在/etc/profile /home/user/.bash_profile 配置了该变量仍未生效

google 了一下: man bash 可以看到一行记录

 Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd,  or  the  secure  shell  daemon
       sshd.   If  bash  determines  it is being run in this fashion, it reads and executes commands from ~/.bashrc, if that file exists and is readable.  It will not do this if invoked as sh.  The --norc
       option may be used to inhibit this behavior, and the --rcfile option may be used to force another file to be read, but rshd does not generally invoke the shell with those options or allow  them  to
       be specified.

3、修改.bashrc 

app_path=/opt/app

4、这样子虽然echo 输出有,但是脚本还是不能获取环境变量, 需要将变量导出

    export app_path

5、ssh 登录的上下文,个人感觉是一个更大的脚本里面,。 (测过如下脚本,确实如此)

  如:  source bashrc

         echo $app_path

         test.sh

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值