Home > Enterprise >  I am not getting difference between ($(cat)) and ${arr[@]}
I am not getting difference between ($(cat)) and ${arr[@]}

Time:12-22

I am learning bash script. While I was solving problems I found out ($(cat)) and ${arr[@]} with something difference. Can anyone please explain what is difference?

#!/bin/bash

read
arr=($(cat))

# read line
# arr=( $line )

arr=${arr[@]}
echo $((${arr// /^}))

my script on which I am working. Also, Why commented section also works as arr=($(cat))?? or there also a difference?? also if possible give one more example so that my concept will clear. Please guide... Thank you

CodePudding user response:

arr=($(cat))

populates an array while

arr=${arr[@]}

populates a scalar variable.

printf '%s %s\n'  a b c d > file
arr=($(cat file))
scalar=${arr[@]}

echo "${arr[1]}"  # b
echo "$scalar"    # a b c d
  • Related