Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Print array of strings – Shell Script

Dears,

Simple question, I have a shell variable with the following values,

myarr=["Life","is","good","when","you","learning"]

It is an array of strings ! Here is how I want to print it. Also, I need to do with a loop because each of these element would be passed to a function to process the string

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Expected Output

Life \
is \
good \
when \
you \
learning

>Solution :

You can make a for loop to print all the elements of your array.

# declare your array variable
declare -a myarr=("Life","is","good","when","you","learning")

# get length of an array
length=${#myarr[@]}

for (( j=0; j<${length}; j++ ));
do
  printf "${myarr[$j]}\n"
done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading