Search code examples
bashsubshell

"basename" used in subshell returns "command not found"


When running this script:

#!/bin/sh -ex

if [[ $# -ne 1 ]]; then
  echo "./import-public-ssh-key.sh <absolute path to public key>"
  exit 1;
fi

PATH=$1
KEY=$(basename ${PATH})

I get:

./import-public-ssh-key.sh: line 9: basename: command not found

without the subshell basename works:

$ basename /Users/mles/.ssh/id_rsa.pub
id_rsa.pub

Why is basename not working in the subshell? I'm on a mac if this is relevant.


Solution

  • You reset the PATH. Don't do that. The shell searches all the directories listed in PATH, and you have changed it so that PATH no longer contains the directory that contains basename.