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

猜你喜欢

转载自blog.csdn.net/wangweidong_1991/article/details/81913864