0

file text.txt:

11
22

test.sh file:

#!bin/bash

mapfile -t data < text.txt

firstVal = printf '%s' "${data[0]}"

printf '%s' $firstVal

exit 0

error: test.sh: line 5: firstVal: command not found

Question: What is the right expression for assigning the data of array to a variable? I've tried using $() too, but same result.

CuriousNewbie
  • 319
  • 4
  • 13
  • You'd have the same problem with *every* assignment when adding spaces around the `=` (making the operation no longer an assignment at all); it has nothing to do with arrays or text files. (In addition to the linked duplicate, please paste your code into http://shellcheck.net/ and follow the links in its analysis). – Charles Duffy Jun 23 '20 at 02:21
  • `firstVal=${data[0]}`, also hard coding an explicit exit status is not a good idea since your script might exit with another value than `0`, just let the script exit with the last command executed but that's just me. – Jetchisel Jun 23 '20 at 02:34

0 Answers0