0

I'm assigning a variable inside a for loop where I make an output file name from the provided input.

I've never had any problems with arrays or for loops before but the behaviour is very strange. I've used similar for loops elsewhere in the code which all work fine. The code below works fine and the variable is assigned as expected.

count=1

for i in "${INPUT[@]}"
do
    local INPUT[$count]=`echo -n "$i" | sed 's/\(.\)/\1\n/g'`

    let count=count+1
done

That all works as expected, however, the code below does not work.

count=1

for i in "${INPUT[@]}"
do
    local OUTFILE[$count]="$i"

    let count=count+1
done

If I echo out the value of $INPUT[$count] or $i I get my expected result, however, when I echo out the value of $OUTFILE[$count] after assignment, it comes out as [1], [2], [3], etc. rather than person's name, person's name, person's name, etc.

9
  • What is the purpose of the local keyword here? Commented Aug 29, 2014 at 14:07
  • 1
    It's inside a function Commented Aug 29, 2014 at 14:07
  • I haven't seen this usage before. Does it turnthe whole array into a local variable? Or just this entry? Is there a reference for this syntax? Commented Aug 29, 2014 at 14:09
  • Does taking it out help? Commented Aug 29, 2014 at 14:09
  • 1
    Unrelated tip: since the array index is evaluated in an arithmetic context, you can increment count "inline" with local OUTFILE[count++]="$i". Commented Aug 29, 2014 at 14:18

2 Answers 2

4

It sounds like you are writing something like

echo $OUTFILE[$count]

when you should be writing

echo ${OUTFILE[$count]}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that was it, I feel rather silly now. It was part of a larger string concatenation as part of a full file path and I had the curly braces misplaced. Thank you!
2

To copy an array into another BASH array you don't need a loop, you can just do:

OUTFILE=( "${INPUT[@]}" )

4 Comments

Thank you! This wasn't the cause of the problem but it's a good thing to know so thank you! Although, I'm curious about this because I haven't seen it in documentation and Vim is highlighting the right outer bracket in red to suggest invalid syntax.
@Kervate There's nothing about this that needs explicit calling out in the documentation as such. It is just normal array construction where the values are being taken from the expansion of all elements of another array. vim shouldn't be mis-highlighting that though (well possibly if not in bash mode I suppose but I think that would be a mistake also).
Yes vim highlighting have many modes and better to select BASH mode to get correct syntax highlighting.
Pretty sure it is in Bash mode because I've used a fair few Bashisms such as "local" and "function" and it syntax highlights all of that fine

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.