Home > database >  bash: using tr to create an array is not creating an array?
bash: using tr to create an array is not creating an array?

Time:07-28

I'm modifying a script I found at https://www.cyberciti.biz/faq/bash-scripting-using-awk/ but stuck on why an array is not being created in the case below. I looked up how to explode a string in bash and many sites say to use tr so I tried:

    FILES="$(ldd $pFILE | awk '{ print $1$2$3 }' | egrep -v ^'/')"

    for i in $FILES
    do
        arr=()
        arr=$(echo $i | tr '=>' ' ')
        
        echo "raw arr[0]=${arr[0]}"
        echo "raw arr[1]=${arr[1]}"
    done

All the results show that arr[1] is blank and arr[0] has the entire line except the => was changed to double space (I though it should have changed it to a single space)

> raw arr[0]=libpthread.so.0  /lib/i386-linux-gnu/libpthread.so.0
> raw arr[1]=

CodePudding user response:

You need to put the values inside () to create an array.

arr=($(echo "$i" | tr '=>' ' '))

There's no need for arr=() before that.

  • Related