$(dirname `readlink -f $0`) 中的奇怪行为

d4v*_*v00 12 bash

当我以普通用户身份运行以下命令时,一切正常:

$(dirname `readlink -f $0`)
Run Code Online (Sandbox Code Playgroud)

但是在我切换到root后,出现了以下错误:

readlink: invalid option -- 'b'
Try `readlink --help' for more information.
dirname: missing operand
Try `dirname --help' for more information.
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我在本地 Fedora 16 和 Amazon EC2 上尝试过,它们都运行 bash shell。

编辑插图。

很抱歉我没有在这里进一步说明这个问题。这是场景:

使用普通用户帐户:

$ pwd 
/home/myuser 
$ export MY_DIR=$(dirname `readlink -f $0`) 
$ echo MY_DIR 
/home/myuser
Run Code Online (Sandbox Code Playgroud)

使用根:

# pwd
/root
# export ROOT_DIR=$(dirname `readlink -f $0`)
readlink: invalid option -- 'b'
Try `readlink --help' for more information.
dirname: missing operand
Try `dirname --help' for more information.

# export ROOT_DIR=echo $(dirname `readlink -f -- $0`)
# echo $ROOT_DIR
/root
Run Code Online (Sandbox Code Playgroud)

enz*_*tib 18

这应该与用户登录 shell 中的错误相同,因为在登录 shell 中,0shell 参数扩展为当前进程的名称,给出-bash了表示登录 shell 的减号。您现在可以看到-b错误的来源。

试试吧

echo "$( dirname "$(readlink -f -- "$0")" )"
Run Code Online (Sandbox Code Playgroud)