3
 echo "Enter N "   # enter N for number of inputs for the loop                                                         
 read N # reading the N
 #using c-style loop
 for((i=1;i<=N;i++))
 do
 read -a arr # arr is the name of the array
 done
 echo ${arr[*]} # 1 
 echo ${arr[@]} # 2   

Tried all the ways to display all the elements of the array but not getting the desired output. It's displaying the last element of the array.

1
  • 1
    Both will display the contents. You hand also use declare -p array and for i in ${array[@]}; do echo $i; done or for ((i = 0; i < ${#array[@]}; i++)); do echo ${array[$i]}; done (you should quote the array if whitespace is included) However, you will only ever read the last element if you attempt to enter the values on separate lines with your loop. Commented Feb 18, 2018 at 16:38

5 Answers 5

9

Hopefully this help other's who have the same issues.

Display all contents of the array in the shell:

"${arr[*]}"

Cleaning up your script (not sure what your intention was, though):

read -p "Enter N " N # User inputs the number of entries for the array
  ARR=() # Define empty array
  #using c-style loop
  for ((i=1;i<=N;i++))
  do
    read -p "Enter array element number $N: " ADD # Prompt user to add element
    ARR+=($ADD) # Actually add the new element to the array.
  done
echo "${ARR[*]}" # Display all array contents in a line.

I found a similar solution from @choroba at: How to echo all values from array in bash

Sign up to request clarification or add additional context in comments.

Comments

2

To be able to populate an array in loop use:

arr+=("$var")

Full code:

read -p 'Enter N: ' N

arr=() # initialize an array

# loop N times and append into array
for((i=1;i<=N;i++)); do
   read a && arr+=("$a")
done

Comments

2

you are reading the data in an array arr and trying to print array

Comments

1

You keep redefining array with read -a. The code should be written like this instead:

#!/bin/bash
echo "Enter N "   # enter N for number of inputs for the loop                                                         
read N # reading the N
#using c-style loop
declare -a array
for((i=1;i<=N;i++))
  do
    read array[$i] # arr is the name of the array
done
echo ${array[*]} # 1 
echo ${array[@]} # 2   

There are probably better ways to doing this. I just wanted to show how to fix your current code.

Example run

$ bash ./dummy.sh 
Enter N 
2
3
4
3 4
3 4

2 Comments

The echo approaches are both buggy -- try entering * as a value in the array; you'll see neither one prints it correctly. declare -p array is best practice if unambiguous output is the top priority. printf '%q\n' "${array[@]}" is a reasonable compromise, as it prints each entry one-to-a-line with values escaped with regular shell quoting; printf '%s\0' "${array[@]}" is the best practice when generating output for machine (not human) consumption, as it emits all values literally, with NULs separating them. (This is particularly unsuitable for humans as terminals don't print NUL).
@StefanBecker Yes when i entered * as input, it's not showing the desired output. Can you please explain why it is happening?
1

The most convenient way of displaying all elements of array is:

printf '%s\n' "${array[@]}"

, inspired by @Charles Duffy's answer.

For example:

array=("arg 1", "arg 2", "...")

Then the printf '%s\n' "${array[@]}" will output

arg 1,
arg 2,
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.