realpath command not found

I have debian squeeze amd64. My current shell is bash. If I write the following in my terminal, it works:

$ uname -a
Linux core 2.6.32-5-amd64 #1 SMP Fri May 10 08:43:19 UTC 2013 x86_64 GNU/Linux
$ echo $SHELL
$ echo $(realpath                                                                                                                                                     

My file looks like this:
echo $(realpath "$1")

If I try to execute the following, I get an error
$ ./ 
./ line 2: realpath: command not found

How can I use the realpath command within a bash file?

Additional Information

$ type -a realpath
realpath is a function                                                                                                                                                                        
realpath ()                                                                                                                                                                                   
    <a href="" class="__cf_email__" data-cfemail="f294cfd6b2">[email protected]</a>;                                                                                                                                                                                     
    if [ -d "$f" ]; then                                                                                                                                                                      
        base="/$(basename "$f")";                                                                                                                                                             
        dir=$(dirname "$f");                                                                                                                                                                  
    dir=$(cd "$dir" && /bin/pwd);                                                                                                                                                             
    echo "$dir$base"                                                                                                                                                                          


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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:

readlink -f -- "<a href="" class="__cf_email__" data-cfemail="0f2b4f">[email protected]</a>"

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

Method 2

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.

Method 3

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

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")

Method 4

$ sudo apt-get install realpath

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments