I’m working with a bash script that has the following syntax
$ declare -a THIS[*]
This seems to be illegal in zsh (I get a “no matches found: THIS[*]” error). Can anyone help me translate this to zsh?
Also – what does the [*] syntax mean? (I know we’re declaring an array, but why the [*]?)
update
To provide an example of where the code is used, and explain how it is valid – I’ve copied a few lines from Eric Engstrom’s post on password free ssh
declare -a SSSHA_KEYS[*]
# --- PARSE ARGS --- #
sssha_parse_args() {
local OPTIND=1
while getopts "xe:k:t:" OPT; do
#echo "$OPT $OPTARG $OPTIND"
case $OPT in
t) SSSHA_ARGS="-t $OPTARG" ;;
e) SSSHA_ENV="$OPTARG" ;;
k) [ -f "${OPTARG}" ] && SSSHA_KEYS[${#SSSHA_KEYS[*]}]="$OPTARG" ;;
x) SSSHA_STOP_ON_EXIT=$OPT
esac
done
shift $(($OPTIND - 1))
# set default key, if none specified
if [ -z "${SSSHA_KEYS[*]}" ]; then
for key in $HOME/.ssh/id_[rd]sa; do
[ -f "$key" ] && SSSHA_KEYS[${#SSSHA_KEYS[*]}]="$key"
done
fi
}
I believe the [*] is being used as some kind of dynamic iterator (as we don’t know how many items it will have later). I’d just like to know of the equivalent declaration in zsh!
Only Bash has
declare. The kshes and zsh havetypeset. That code is nonsensical in all of them, and since shells disagree about the parsing of the arguments to declaration commands likedeclareandtypeset(with Bash in particular,-aalters parsing in a specific way), it’s going to do a different unpredictable, potentially dangerous thing in each.Specifically how Bash is interpreting this – from the manpage:
So since it’s unquoted – if there’s a file in the current directory named
THIS*then Bash would throw an illegal name error due to the pathname expansion. Otherwise it will just create an empty array named “THIS”.What
[*]means depends upon context. Unquoted in an ordinary command evaluation context, it’s a character class matching only literal asterisks. If used in a parameter expansion following an array name, it expands all elements of the array to a single word separated by the first character of IFS.declarein bash declares variables, returns their values, sets attributes, and affects function scope. See: http://wiki.bash-hackers.org/commands/builtin/declareedit: Given the example that’s now in the question, in bash the behavior is as described above. The
[*]should be deleted from thedeclare. That code has a number of other issues such as attempting to put arguments into strings, using all-caps variable names, and using[instead of[[in a script that’s clearly not intended to run on a minimal POSIX sh.