1

I have a variables.js file that includes several variables with values like:

var var_1 = 1;
var var_2 = 'someText';

I need to pass these values to variables within the vars bash file:

VAR_1
VAR_2

That sounds like a decently simple task, however, having a lack of practical experience with bash scripts baffles me.

Tried using source or reading line by line, however, it didn't lead to a possible solution.

Moveton
  • 37
  • 7

1 Answers1

1

With GNU grep and a Perl-compatible regular expression (-P):

VAR_1=$(grep -Po '(?<=^var var_1 = ).*?(?=;)' variables.js)
VAR_2=$(grep -Po '(?<=^var var_2 = ).*?(?=;)' variables.js)

See: The Stack Overflow Regular Expressions FAQ -> Lookarounds

Cyrus
  • 84,225
  • 14
  • 89
  • 153