Inspired by this answer: When I run type -p
in the command prompt, it reliably tells me the path if the command exists:
pi@raspberrypi:~ $ type -p less
/usr/bin/less
pi@raspberrypi:~ $ type -p asdf
pi@raspberrypi:~ $
However, when used in a in a script it is as if the -p
parameter is interpreted as a command of itself. It seems the type
command ignores it as its option, because there is always some rogue text of -p not found
in the result. It breaks the rest of the script:
#!/usr/bin/sh
main() {
for mycommand in $1; do
echo Checking $mycommand
loc="$(type -p "$mycommand")"
echo $loc
if ! [ -f "$loc" ]; then
echo I think I am missing $mycommand
fi
done
}
main "less asdf"
Output of the script:
Checking less
-p: not found less is /usr/bin/less
I think I am missing less
Checking asdf
-p: not found asdf: not found
I think I am missing asdf
Can you please help me out here? Is there something about the shell on a raspberry pi that is causing this?
"$1"
,"$mycommand"
,"$loc"
, etc. (3) If you’re getting inconsistent behavior from shell builtin commands, perhaps you’re using different shells. Quite possible (probably?) your interactive shell is bash./usr/bin/sh
is probably not bash. Try writing the script with#!/bin/bash
,#!/usr/bin/bash
, or#!/usr/bin/env bash
as the first line. – G-Man Says 'Reinstate Monica' Mar 22 '22 at 08:11sh
is a standard language for which there exist several interpreter implementations such asbash
,ksh
ordash
, like C is a standard language for which there exist several compiler implementations such as gcc or clang (implementations of thecc
standard command). bash supports extensions to that language, like gcc support extensions. – Stéphane Chazelas Mar 22 '22 at 08:26