[已解决]echo 'export abc=123' | source /dev/stdin 为何无法设置变量?

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
九天星
帖子: 1440
注册时间: 2007-07-14 20:45

Re: echo 'export abc=123' | source /dev/stdin 为何无法设置变量?

#2

帖子 九天星 » 2017-08-01 15:55

因为这是一个字符串
科学之子
帖子: 2284
注册时间: 2013-05-26 6:58
系统: Debian 9

Re: echo 'export abc=123' | source /dev/stdin 为何无法设置变量?

#3

帖子 科学之子 » 2017-08-01 21:58

九天星 写了:因为这是一个字符串
不是您理解的这样

代码: 全选

$ help source
source: source filename [arguments]
    Execute commands from a file in the current shell.
    
    Read and execute commands from FILENAME in the current shell.  The
    entries in $PATH are used to find the directory containing FILENAME.
    If any ARGUMENTS are supplied, they become the positional parameters
    when FILENAME is executed.
    
    Exit Status:
    Returns the status of the last command executed in FILENAME; fails if
    FILENAME cannot be read.
source命令是在当前shell执行命令,命令执行环境跟用户手动输入执行时是一样的
本意是想把echo的输入重定向给source去执行,但管线(Pipiline)会再开一个新的shell去执行source
"Process Substitution"(<(list))的结果是list的标准输出文件
例如:

代码: 全选

$ echo <(date)
/dev/fd/63
此帖的提问主要是为了解决这个问题.
头像
九天星
帖子: 1440
注册时间: 2007-07-14 20:45

Re: [已解决]echo 'export abc=123' | source /dev/stdin 为何无法设置变量?

#4

帖子 九天星 » 2017-08-02 18:09

你知道还问
回复