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.
PATH
has special meaning. This is why you should use lowercase names for your own variables, to avoid overwriting ones that change shell or OS behavior by mistake. That is to say:path=$1
won't risk any issues. – Manamanaclesh -e
is... controversial; it can easily cause more bugs than it prevents, including oddly context-sensitive ones that evade easy attempts at testing. See the exercises in BashFAQ #105 and the list of incompatibilities between different implementations at in-ulm.de/~mascheck/various/set-e before deciding to use it. – Manamanacle#!/bin/bash
, not#!/bin/sh
, as your shebang; using ash
shebang doesn't guarantee you'll have any language features available except those given in the POSIX shell language spec). – Manamanaclebasename
-- thus,key=$(basename "$path")
-- to work correctly with paths having spaces, or when IFS is otherwise set to contain characters that can exist inside your filenames; the curly braces make no difference here one way or the other, but quotes act to prevent string-splitting and glob expansion). – Manamanaclepath
has a special meaning in C-shell, but I guess we won't stoop that low. – Shoemaker