bash
我的 bash 配置文件中有一个特定的代码:
$ cat ~/.bash_profile
#!/usr/bin/env bash
echo "SHELL: $SHELL"
function printfiles() {
while IFS='' read -r _file || [[ -n "$_file" ]]; do
echo "file: ${_file}"
done < <(ls)
} && export -f printfiles
Run Code Online (Sandbox Code Playgroud)
的< <(FUNCTION)
是bash
特定的语法,不支持的sh
。当我使用 ssh 登录到远程机器时:
$ ssh my.remote
me@xxx.xx.xxx.xx's password:
Last login: Fri Nov 17 11:27:39 2017 from yyy.yy.yy.yy
sourcing /home/me/.bash_profile
SHELL: /bin/bash
Run Code Online (Sandbox Code Playgroud)
它工作正常。现在我想再次登录但转发我的X11
:
$ ssh -X my.remote
me@xxx.xx.xxx.xx's password:
sh: printfiles: line 2: syntax error …
Run Code Online (Sandbox Code Playgroud) 我有一个 bash 文件,src-useful.bash
包含有用的函数,例如say_hello()
,位于 /path/to/useful 中。
在我的中~/.bash_profile
我添加了以下几行:
export BASH_USEFUL=/path/to/useful
source $BASH_USEFUL/src-useful.bash
Run Code Online (Sandbox Code Playgroud)
打开新终端我可以检查以下内容:
$ echo $BASH_USEFUL
/path/to/useful
$ cat $BASH_USEFUL/src-useful.bash
function hello() {
echo "hello!"
}
$ hello
hello!
Run Code Online (Sandbox Code Playgroud)
我创建了一个脚本say_hello.sh
:
$ cat say_hello.sh
echo "BASH_USEFUL: $BASH_USEFUL"
hello
$ ./say_hello.sh
BASH_USEFUL: /path/to/useful # Recognizsed
say_hello.sh: line 2: say_hello: command not found # Not recognized?
Run Code Online (Sandbox Code Playgroud)
如果我来源$BASH_USEFUL/src-useful.bash
它say_hello.sh
会工作但是:
$ cat say_hello.sh
echo "BASH_USEFUL: $BASH_USEFUL"
source $BASH_USEFUL/src-useful
say_hello
$ ./say_hello.sh
BASH_USEFUL: /path/to/useful # Recognized
hello! …
Run Code Online (Sandbox Code Playgroud)