Sample Header Ad - 728x90

Reading variable from a txt file using bash

0 votes
1 answer
996 views
I'm new to bash. I'm trying to write a script that will read data from a text find and declare some variables. In the example below we read from a tab delimited file "ab.txt" that looks like this: a->AA b->BB Where -> denotes a tab. I'm reading this data with this code:
#!/bin/bash
while read line
do
	tmp=(${line///})
	fieldName=${tmp}
	
	case $fieldName in
	"a")
		a=${tmp}
		;;
	"b")
		b=${tmp}
		;;
	esac
		
done < "ab.txt"


echo "a:"
echo $a	

echo "b:"
echo $b

echo "concat a,b"
echo $a$b

echo "concat b,a"
echo $b$a
This gives me "a" and "b" nicely, but will not concatenate a and b! The output is this:
a:
AA
b:
BB
concat a,b
BB
concat b,a
AA
What am I doing wrong?
Asked by Adi Ro (101 rep)
Jan 8, 2020, 07:04 PM
Last activity: Jan 9, 2020, 08:16 AM