lingtalfi / printScriptDir

get the bash script's real directory
6 stars 1 forks source link

line 17 not POSIX compliant, bad substitution #1

Open modoj opened 8 years ago

modoj commented 8 years ago

printScriptDir.sh: 17: printScriptDir.sh: Bad substitution

16 17 elif [ '/' = "${1:0:1}" ]; then 18

I felt I should mention that this line will not work in alternative shells, that rely more heavily on POSIX compliance, such as zsh, and obviously sh. I know it's stated it's a bash script, but since it's recommended to use this function in other scripts, it's worth noting. A work-around would be to call bash when running the script from another shell ($ bash printScriptDir.sh )

The rest of the script works fine regardless of shell, just offending line 17 causes issue.

It's always best to avoid bashisms

see link below for reference https://github.com/koalaman/shellcheck/wiki/SC2039

lingtalfi commented 8 years ago

I wrote this simple script.

#!/bin/zsh

if [ '/' = "${1:0:1}" ]; then
    echo "pou"        
fi   

echo doo

And I tested it with sh and zsh on my mac, with this commands (typed on a terminal):

> sh
> ./test.sh /mm
pou
doo
> zsh
\W > ./test.sh /dd
pou
doo

Thank you for showing interest in this project, but I cannot (don't know how to) reproduce the problem (i.e. the condition line 17 seems to behave as expected on my mac).

Are you on windows? What's your setup?