Bash脚本:非法别名

15t*_*ers 2 bash shell

我正在搞乱我的.bash_profile文件(Mac OSX),当我尝试设置别名时:

别名.=".〜/ .bash_profile"

我随后无法获取我的.bash_profile文件(退出文件后键入"source .bash_profile"并停留在非终止进程中).当我将别名更改为:

别名s =".〜/ .bash_profile"

一切都很好.这是怎么回事?

kon*_*box 6

当调用的文件(在这种情况下,~/.bash_profile)也使用时.,会发生递归调用,因为它.会再次引用同一个文件.

假设是否~/.bash_profile尝试~/.bashrc使用此行调用:

[[ -f ~/.bashrc ]] && . ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

该命令将首先作为别名展开,它将改为:

[[ -f ~/.bashrc ]] && . ~/.bash_profile ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

这将使真正的.呼叫~/.bash_profile,而不是,只是有~/.bashrc一个位置参数变量.这使得无限循环.

重现:

  1. ~.bash_profile必须存在并包含一个调用另一个文件的行..
  2. alias .="echo -n x; . ~/.bash_profile"; ..

它应该产生无限,x直到shell崩溃.