Reputation: 1404
I have this bash code that replace some variables in a template text file and write the result somewhere. This works :
#!/usr/bin/env bash
PROJECTFOLDER=$1
USER=$2
export PROJECTFOLDER USER
CONFIGVARS='$PROJECTFOLDER:$USER'
envsubst "$CONFIGVARS" < template.conf > /home/me/config.inc.php
and a template.conf like so :
<h1>${USER}</h1>
<h2>${PROJECTFOLDER}</h2>
When called with :
./script foo bar
This gives :
<h1>foo</h1>
<h2>bar</h2>
But if I want to save the output to a folder with sudo or root it does not work, as seen here.
What I should do is running the command in a sudo shell (I don't want to use the tee or dd method to keep it simple).
sudo sh -c 'envsubst "$CONFIGVARS" < template.conf > /etc/config.inc.php'
But whatever combination of quotes ands brackets I try, it gives either
<h1>${USER}</h1>
<h2>${PROJECTFOLDER}</h2>
or nothing :
<h1></h1>
<h2></h2>
Solution Thanks to l0b0 :
envsubst "$CONFIGVARS" < template.conf | sudo tee /etc/config.inc.php
Upvotes: 1
Views: 891
Reputation: 58848
What you'll want to do is pipe the command to sudo tee /home/me/config.inc.php
.
Upvotes: 3