0

I would like to fill a simply array in bash inside a while loop.

I try to do this :

read -p " Value : " nb

declare -a array

while [[ $nb != "s" ]]
do
    read -p " Value : " nb
    array+=("$nb")
done

echo ${array[@]}

If I try with 1,2,3,4 and 5 as values, the output is :

Value : 1
Value : 2
Value : 3
Value : 4
Value : 5 ( to stop the loop and display the array )
2 3 4 5 s

Or, I wan this output :

Value : 1
Value : 2
Value : 3
Value : 4
Value : 5
Value : s 
1 2 3 4 5

Can you tell me what is wrong in my script ?

2
  • Your code does a read, then it does an append, and then it goes up to to do the test to decide if it should run the loop again. Thus, the append happens whether or not that test would succeed. Commented Jul 1, 2019 at 20:39
  • There's nothing at all bash-specific about this -- you'd have the exact same behavior with C's while loop, or Python's, or Java's, etc. Commented Jul 1, 2019 at 20:40

2 Answers 2

1

The two lines of code inside your while loop need to be swapped.

read -p " Value : " nb

declare -a array

while [[ $nb != "s" ]] do
    array+=("$nb")
    read -p " Value : " nb
done
echo ${array[@]}

Now your first read is put into your array and your last read (to exit the loop) is not put into the array.

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

1 Comment

BTW, I'd suggest quoting "${array[@]}"; that way a * won't be replaced with a list of filenames. You might also use the -r argument to read so backslashes don't get munged.
0

Your first read is not adding the input to your array. So just keep your read inside the while loop. Then only add the input to the array if it does not equal s.

declare -a array

while [[ $nb != "s" ]]; do 
  read -p "Value: " nb
  if [[ $nb != "s" ]]; then 
    array+=($nb)
  fi 
done 

echo ${array[@]}

Update: terser syntax, thanks to comment by Charles Duffy.

declare -a array

while :; do 
  read -p "Value: " nb
  [[ $nb == s ]] && break
  array+=($nb)
done 

echo ${array[@]}

1 Comment

Why have the first test at all? Just put [[ $nb = s ]] && break after the read, and change while [[ $nb != "s" ]]; do to while :; do; then you can get rid of the if.

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.