Linux command “which” doesn’t locate executable files on NFS path under Ubuntu

linuxnfsUbuntuwhich

I noticed the Linux shell command 'which' does NOT show an executable file although it is alredy on the search path.

More specifically, my $PATH includes '~/.local/bin'; an executable file is ~/.local/bin/charm. 'which charm' returns nothing although I can run 'charm' with no problem. I am using Ubuntu 16.04 and bash

The issue depends on Linux distribution. If I log on to another machine of RHEL 6.10, the command 'which charm' works fine for the same executable.

The issue may also relate to nfs mount. On Ubuntu 16.04, the command 'which' seems to work fine for executables on local disks, for example, 'which which' shows '/usr/bin/which', but it does NOT find execuatables on nfs paths, which is the case for my '~/.local/bin'.

I searched online but couldn't find exaplaination. Is there a way to make the command 'which' under Ubuntu show all mached executables on path including nfs mounts?

Thanks!

Best Answer

Your $PATH includes ~/.local/bin where tilde hasn't been expanded. bash will expand it on the fly each time, but which (at least on Ubuntu) or sh won't.

If you want which to see the executables in ~/.local/bin you need to make sure tilde is expanded before the PATH variable gets it. The string ~/.local/bin is probably added to your PATH by one of the startup scripts. Find the relevant line and fix it according to the examples below.


Note: first run each of the following examples in a fresh shell, so you can observe (echo "$PATH") how it changes the variable each time independently.

This won't expand tilde character (because of quoting):

PATH="~/.local/bin:$PATH"

This will:

PATH=~/".local/bin:$PATH"

Or this:

PATH=~/.local/bin:$PATH

Rules of tilde expansion are somewhat complicated. Consider using $HOME. Examples:

PATH="$HOME/.local/bin:$PATH"
PATH="$PATH:$HOME/.local/bin"

To fix an already established PATH variable, replace ~ in it with the content of $HOME:

PATH="${PATH/\~/$HOME}"
Related Question