-1

How can I make this work?

I have test data: test.dat

red apples
green tomato
yellow lemon

I've tried numerous examples that I could find to no avail.

Obviously there is something about the shell I don't understand...

#!/bin/bash

#lines=$(IFS='\n' cat $1)
#IFS=$'\r\n' GLOBIGNORE='*' command eval  
#lines=($(cat test.dat))
#IFS=$'\n' read -d '' -r -a lines < test.dat
#readarray -t lines < test.dat
#lines=$(cat test.dat)

lines=$(< test.dat)


for line in $lines
do
  echo "${line}"
done

printf "line 1: %s\n" "${lines[1]}"
echo "line 2: ${lines[2]}"

Output:

./test.sh test.dat
red
apples
green
tomato
yellow
lemon
./test.sh line 71: lines[1]: unbound variable
duffy
  • 81
  • 5

1 Answers1

0

Using the readarray command should work, readarray -t a < /path/to/filename

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5