sciencenmagic
sciencenmagic

Reputation: 103

bash - how to build paths using multiple variables

I'm trying to build multiple paths by a file.txt with two columns as variables

a   b
c   d
e   f
g   h

I want to replace the path with that variables, as follows:

/home/user/var1/var2/
/home/user/a/b/
/home/user/c/d/
/home/user/e/f/
/home/user/g/h/

I've tried to do a double loop, but it returns all the possible combinations by the two variables

var1 = (cut -f1 file.txt)
var2 =(cut -f2 file.txt)

for i in $var1
do
for j in $var2
do

echo home/user/$i/$j/

done
done

I'm new to bash and would really like some help

Upvotes: 0

Views: 742

Answers (1)

Barmar
Barmar

Reputation: 780879

You don't need two loops, that will produce a cartesian product of all the names. Just loop once with two variables:

while read -r var1 var2; do
    echo "home/user/$var1/$var2/"
done < file.txt

Upvotes: 3

Related Questions