This modification of your script
- captures the remaining command line items into the array;
- reports the array size; and
- reports the value assigned to the individual array
elements
The script is as follows:
#!/bin/bash
k="$1"
shift
declare -i x
# Assigning values from command line
x=( "$@" ) ### edited assignment command
echo "Assigned array items: ${x[@]}"
size=${#x[*]}
echo "Array size = ${size}"
i=0
max=$((${size}-1))
while [ $i -le ${max} ]
do
echo "x[$i] = ${x[$(expr $i )]}"
((i+=1))
done
Note that there is no necessity to use declare in scripts where input data is of known type (i.e. position 2-6 of your inputs. Most programs work with data in a predefined known format. Even without using typing, those would normally check for "empty value" conditions.
"declare" would only be used where
- input types are of unknown type, AND
- the consequences of an invalid type would have severe impact in terms of the usage of the invalid data to contain the path for actions (i.e. rm -rf ${variable}) or would lead to manipulation/corruption of the incorrect partition (i.e. ${partition} == 0 during a semi-automated process to modify/repair with fsck or parted.