Strange behavior in $(dirname `readlink -f $0`)

This should be the same error as in a user login shell, because in a login shell the 0 shell parameter, expanding to the name of the current process, gives -bash, the minus indicating the login shell. You now see where the -b error comes from.

Try instead

echo "$( dirname "$(readlink -f -- "$0")" )"

If you really want the directory name of the shell script which is being run:

script_dir="$(dirname -- "$(readlink -f -- "$0")")"

Yes, it's a bit cludgy, but it's safe.

If you want the current shell, you can try @MichaelMrozek's suggestion of using $SHELL.

Tags:

Bash