Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - get variables from an external file

I'm trying to make a configuration file to help running a bash script.

The idea is having a file (script.conf) like this

directory=c:/path/to/a/specific/directory/
logo=y
title=y
hotspots=n
combobox=n

Then, running the script, it will read script.conf and get those variables to use in the script.

How can I do that?

like image 581
RafaelGP Avatar asked Sep 02 '25 11:09

RafaelGP


1 Answers

The source command (also known as ., but not to be confused with the directory of the same name) will allow you to run another file in the current shell. Simply make that file contain variable assignments.

foo.sh

#!/bin/bash
. bar.sh
echo "$baz"

bar.sh

baz=42
like image 153
Ignacio Vazquez-Abrams Avatar answered Sep 04 '25 07:09

Ignacio Vazquez-Abrams