Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have a text file named "raj.txt" containing following content:
raj magesh popey ravi
How can I assign each word to array element? a[0]=raj a[1]=magesh a[2]=popey a[3]=ravi
a[0]=raj a[1]=magesh a[2]=popey a[3]=ravi
Try bash:
bash
while IFS= read -r line do set -- $line echo "$1" echo "$2" done < file
Add a comment
If your shell support array, like bash, zsh, ksh93, try:
zsh
ksh93
$ array=($(<filename)) $ printf '%s\n' "${array[0]}" raj $ printf '%s\n' "${array[1]}" magesh $ printf '%s\n' "${array[2]}" popey $ printf '%s\n' "${array[3]}" ravi
Required, but never shown
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.
Explore related questions
See similar questions with these tags.