I have faced the same issue when we tried to run spark3 in CDP cluster where spark2 also running as default.
Error:
/opt/cloudera/parcels/SPARK3/lib/spark3/bin/spark-class: line 101: CMD: bad array subscript
Adding env variable in the code before launching the spark launcher job did not work, so finally updated
/etc/profile.d/cdh.sh by adding below entries
export HADOOP_CONF_DIR=/opt/cloudera/parcels/SPARK3/lib/hadoop/etc/hadoop
export SPARK_CONF_DIR=/etc/spark3/conf
export SPARK_HOME=/opt/cloudera/parcels/SPARK3/lib/spark3
Above env variables can be printed out through printenv command.