realpath command not found

There are at least two programs called realpath:

  • An old program from back when GNU coreutils didn't include readlink -f. It is now deprecated in favor of readlink -f, so many distributions have stopped carrying it.
  • The realpath program introduced in GNU coreutils 8.15. This is too old to be in Debian squeeze or even wheezy; at the time of writing, Debian unstable doesn't ship it either. This program is very close to readlink -f.

For some reason, you have a shell function which partly emulates the behavior of realpath. This emulation is partial: if you call it on a symbolic link, it doesn't follow the symbolic link.

Since this is a shell function, presumably loaded from or via .bashrc, it's only available to code running in your interactive shell. If you want it to be available to other programs, assuming you're running Linux, create a script that emulates realpath:

#!/bin/sh
readlink -f -- "$@"

(This doesn't emulate realpath's rarely-used command line options.)


It works only in shell, because script file has different scope and doesn't have access to your local functions and aliases defined in your rc files. And realpath command actually doesn't exist in your system.

So either install realpath from the package, define your own function (as part of the script, check some examples) or source the rc file in your script where it's defined (e.g. . ~/.bashrc).


Here are the steps to install realpath if it's not present:

  • Ubuntu: sudo apt-get install coreutils
  • OS X: brew install coreutils

Ubuntu & Debian

On Debian or Ubuntu it seems the realpath should be installed by default. I've checked in the recent Debian 8 (Jessie) and it seems to have coreutils installed by default.

Tested using fresh VM images:

$ vagrant init debian/jessie64 && vagrant up --provider virtualbox && vagrant ssh
$ vagrant init ubuntu/vivid64 && vagrant up --provider virtualbox && vagrant ssh

Then in VM:

$ type -a realpath
realpath is /usr/bin/realpath

Instead of realpath, you can also use readlink -f file (or greadlink) provided by coreutils package as well.


Is realpath a actual command or a script? I would check to see where it is coming from.

$ type -a realpath

I'm not familiar with this tool, and so it's likely not part of your normal distribution, perhaps it's installed in a non-standard location which isn't present on Bash's $PATH but is within your login environment's $PATH.

In any event, the above type command will show you where the command is coming from, at which point you can alter the method you're calling it in your script like so:

echo $(/path/to/realpath test.sh)

Or amend your script's $PATH so that it also includes this non-standard location.

Functions in the shell

Much of your environment does not get called when you invoke a shell script. If you think about this, this makes a lot of sense, since you generally don't want scripts to have all the additional baggage that a user's environment may have.

You can either determine which source file is providing this function and either source it, or simply instruct Bash to incorporate your login environment.

#!/bin/bash -l
echo $(realpath "$1")

Tags:

Linux

Bash

Debian