Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a Bash variable assignment from other file

I have this test script:

#!/bin/bash
echo "Read a variable" 
#open file
exec 6<test.txt
read EXAMPLE <&6
#close file again
exec 6<&-
echo $EXAMPLE

The file test.txt has only one line:

EXAMPLE=1

The output is:

bash-3.2$ ./Read_Variables.sh
Read the variable
EXAMPLE=1

I need just to use the value of $EXAMPLE, in this case 1. So how can I avoid getting the EXAMPLE= part in the output?

Thanks

like image 801
Marcos Griselli Avatar asked Dec 17 '25 09:12

Marcos Griselli


1 Answers

If the file containing your variables is using bash syntax throughout (e.g. X=Y), another option is to use source:

#!/bin/bash
echo "Read a variable" 
source test.txt
echo $EXAMPLE
like image 152
Joe Holloway Avatar answered Dec 20 '25 01:12

Joe Holloway