Recently the script was working fine, but from some days I'm receiving such message, while running the readlink -f "$0" command:

readlink: illegal option -- f
usage: readlink [-n] [file ...]

I was running the following code to debug:


RLPATH=`which readlink`
RLOUT=`readlink -f -- "${0}"`
DIROUT=`dirname -- ${RLOUT}`

echo "dir: ${DIR}"
echo "path: ${PATH}"
echo "path to readlink: ${RLPATH}"
echo "readlink output: ${RLOUT}"
echo "dirname output: ${DIROUT}"


# ./debug.sh
readlink: illegal option -- f
usage: readlink [-n] [file ...]
usage: dirname string [...]
dir: /home/svr
path: /sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin
path to readlink: /usr/bin/readlink
readlink output:
dirname output:

What is wrong ?

  • May be readlink binary was overwritten? Check, that stat and readlink has same inode: ls -li /usr/bin/readlink /usr/bin/stat
    – citrin
    Dec 7 '12 at 10:45
  • If you wan to know real directory, where script located, better to use: dirname $(realpath "$0")
    – citrin
    Dec 7 '12 at 10:47
  • @citrin Yes: 1083670 -r-xr-xr-x 2 root wheel 16760 May 4 2011 /usr/bin/readlink 1083670 -r-xr-xr-x 2 root wheel 16760 May 4 2011 /usr/bin/stat
    – Scott
    Dec 7 '12 at 11:04
  • @citrin # dirname $(realpath "$0") Illegal variable name.
    – Scott
    Dec 7 '12 at 11:52
  • 1
    8.2 has the -f option, so your stat binary is modified. I suggest to rebuild and install it: cd /usr/src/usr/bin/stat && make install
    – arved
    Dec 10 '12 at 11:13

This is an issue with the version of FreeBSD that you're running. Older versions do not support the -f option.

The easiest way to get around this is to use the GNU coreutils port (/usr/ports/sysutils/coreutils) that will give you "greadlink". This is what I use in my scripts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.