0

Can anyone helps with my problem with arrays in BASH? I have this code:

i=1
cat test.txt | while read LINE; do
    string=$(echo $LINE | sed -e 's/\(^[^=]*\):[^=]*$/\1 /')
    log_content[$i]="$string"
    echo -e "\t $i) ${log_content[$i]}"
    i=$(expr $i + 1)
done
pattern=$(echo ${log_content[1]})   - this is zero :(

When I use ksh instead of bash everything works fine. When I use BASH (which aj want to use because of many others purposes), variable "pattern" desnt have any value. Even when I want to show whole content of "log_content" array, there is nothing. Thanks a lot.

1 Answer 1

1

Your log_content variable is being filled inside a subshell that runs your while loop so the value of log_content variable in the outer shell never changes.

To mitigate this you should avoid creation of subshell by using input redirection instead of pipe:

while read LINE; do
  ...
done < test.txt

This should work.

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

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.