Array declaration in Shell script

this is my code

        declare -a USERCERT
        declare -a CACERT
        declare -a KEYSRC

this is the error

+ declare -a USERCERT
./clone.sh: 1: declare: not found
+ declare -a CACERT
./clone.sh: 1: declare: not found
+ declare -a KEYSRC
./clone.sh: 1: declare: not found

Suggest resolution....

# ksh
typeset -A array 
# bash
declare -a array

declare is a bashism not found in other shells. In ksh, use typeset .

And most shells do not have arrays at all.

i need it in shell script....

Is it not possible to define my own array name?

What is your shell ? ksh, bash, ...
And most of use case, you don't need to define array. Use it.

a[1]=value
a=( * )
set -A array  -- $*
1 Like

bash...

so the array size doesnt't matter?

set -A array  -- $*

what does this line do?

Yes ... if you are using a shell which supports arrays.

First, don't declare the arrays; just assign values to them.

---------- Post updated at 01:21 AM ---------- Previous update was at 01:13 AM ----------

If you were running the script in bash, you wouldn't have seen those error messages.

No (so long as there is enough memory).

In bash, there is no -A option to set.

In ksh, it assigns all the words (not the arguments) on the command line as elements in the array named array .

The bash equivalent (which also works in ksh93) is:

array=( $* )

Though what was probably meant was:

set -A array  -- "$@"

Which should be written:

array=( "$@" )
1 Like

Ok..ill try once more....thanks...

---------- Post updated at 04:38 PM ---------- Previous update was at 12:09 PM ----------

this is my code :

       cert=`cat $usr_cert`

                                for i in $cert ; do
                                        USERCERT[$COUNT]=$i
                                        ((COUNT++))
                                done

this is the error I'm getting:

File name too long
+ COUNT++
./clone.sh: 1: COUNT++: not found
./clone.sh: 1: Bad substitution

please explain the error and suggest a resolution....:confused:

COUNT=0

while read i; do
  USERCERT[$COUNT]="$i"
  COUNT=$(( $COUNT + 1 ))
done < $usr_cert
while IFS= read -r i
do
  USERCERT+=( "$i" )
done < "$usr_cert"

Or:

IFS=$'\n'
USERCERT=( $( < "$usr_cert" ) )

Or (bash 4):

mapfile -t USERCERT < "$usr_cert"

what does this error indicate:

./clone.sh: 1: Bad substitution

Probably that you used an invalid variable name in a parameter expansion.