I have some bash code that i am running in a zsh shell on macos.
Code seems to work fine when i run in the shell, but when i run the file from my path, it only brings back the initial and not the initial surname, any help most appreciated.
user="Steve Thomas"
dbuser=$user
initial="${user%"${user#?}"}"
userie=( $( echo $dbuser | cut -d' ' -f1- ) )
userlastname=${userie[2]}
fulluser="${initial}${userlastname}"
echo $fulluser
when run in shell i get what is expected
SThomas
and when i run as file.sh from path i get..
S
Not sure what i am doing wrong here, please advise.
CodePudding user response:
In bash, arrays are indexed from 0, not 1 like in zsh. So, to make the script work in bash, change line 5 to
userlastname=${userie[1]}
You can even make it universal for both the shells:
startindex=2
if [[ $BASH_VERSION ]] ; then
startindex=1
fi
...
userlastname=${userie[startindex]}