Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing bash to expand variables in a string loaded from a file

I am trying to work out how to make bash (force?) expand variables in a string (which was loaded from a file).

I have a file called "something.txt" with the contents:

hello $FOO world 

I then run

export FOO=42 echo $(cat something.txt) 

this returns:

   hello $FOO world 

It didn't expand $FOO even though the variable was set. I can't eval or source the file - as it will try and execute it (it isn't executable as it is - I just want the string with the variables interpolated).

Any ideas?

like image 357
Michael Neale Avatar asked May 21 '12 10:05

Michael Neale


People also ask

How do you expand a variable in bash?

Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. Show activity on this post.

How do you eval in bash?

Examplel-2: Execute `expr` command using `eval`Create a bash file named evaltest.sh and add the following script. This script will assign two integer values into the variable $x and $y. The `expr` and `echo` commands are assigned into two variables, $c1 and $c2 which are executed later by using `eval` command.

How do I expand a variable in Bash?

Bash expand variable in a variable I stumbled on what I think is THE answer to this question: the envsubst command: If you would like to continue work on the data in a file destination.txt, push this back to a file like this: In case it's not already available in your distro, it's in the GNU package gettext.

What is Bash variable in string?

In this topic, we are going to learn about Bash Variable in String. In the programming world, the variable is thought to be an advanced programming concept, where the programmer would use variable only when the value is not known to the code from the start. For example, if we write a program to calculate the sum of 10 & 20.

What is variable expansion in JavaScript?

Any variable that follows a dollar sign is attempted to be replaced with the value of the variable stores. This technique is known as variable expansion. In case it is not able to reference the variable it will throw out an error. We will discuss a small deviation in the next section, but first, let us look at what’s correct and what’s wrong?

What is variable expansion in Python?

This technique is known as variable expansion. In case it is not able to reference the variable it will throw out an error. We will discuss a small deviation in the next section, but first, let us look at what’s correct and what’s wrong? What is correct? What is not correct? echo Var_1 [This will just print the variable name i.e. Var_1]


1 Answers

I stumbled on what I think is THE answer to this question: the envsubst command.

envsubst < something.txt 

Example: To substitute variables in file source.txt and write it to destination.txt for further processing

envsubst < "source.txt" > "destination.txt" 

In case it's not already available in your distro, it's in the GNU package gettext.

@Rockallite

  • I wrote a little wrapper script to take care of the '$' problem.

(BTW, there is a "feature" of envsubst, explained at https://unix.stackexchange.com/a/294400/7088 for expanding only some of the variables in the input, but I agree that escaping the exceptions is much more convenient.)

Here's my script:

#! /bin/bash       ## -*-Shell-Script-*- CmdName=${0##*/} Usage="usage: $CmdName runs envsubst, but allows '\$' to  keep variables from     being expanded.   With option   -sl   '\$' keeps the back-slash.   Default is to replace  '\$' with '$' "  if [[ $1 = -h ]]  ;then echo -e >&2  "$Usage" ; exit 1 ;fi if [[ $1 = -sl ]] ;then  sl='\'  ; shift ;fi  sed 's/\\\$/\${EnVsUbDolR}/g' |  EnVsUbDolR=$sl\$  envsubst  "$@" 
like image 101
LenW Avatar answered Oct 20 '22 08:10

LenW