What I want to achieve is to automatically install NPM packages, so I created a separate variable which holds "sudo npm install"
and iterating over an array which has the package name.
Facing error that command not found but if I put it into the console it works.
npm_base='sudo npm install '
npm_scripts=("-g eslint")
for element in ${npm_scripts[@]}
do
${npm_base}${element}
done
You're very close.
You need to add double quotes around the array, otherwise
-g
andeslint
will be separated.See https://www.cyberciti.biz/faq/bash-for-loop-array/