I learned about envsubst. I wish I knew about this before. This utility can substitute environment variables in a string that come from stdin.
I don't have many use cases for this but I think it would be very useful in config files that I share across multiple machines.
Quick example. Somewhere in a github repo I have a file called .npmrc, which I copy to any new machine I get. Inside that file I have this.
prefix=/absolute/path/to/npm/packages
I don't like it, I want to replace that absolute path with this.
prefix=$XDG_CONFIG_HOME/npm/packages
So, what I can do is create a template file and then create .npmrc from that.
envsubst < ~/templates/npmrc > ~/.npmrc
The result on linux should be this:
prefix=/home/user/.config/npm/packages
And if I wanted, I think I could use curl to fetch the content of the file and then pipe it to envsubst. So technically I don't even need the template in my filesystem.
I learned about envsubst. I wish I knew about this before. This utility can substitute environment variables in a string that come from stdin.
I don't have many use cases for this but I think it would be very useful in config files that I share across multiple machines.
Quick example. Somewhere in a github repo I have a file called
.npmrc, which I copy to any new machine I get. Inside that file I have this.I don't like it, I want to replace that absolute path with this.
So, what I can do is create a template file and then create
.npmrcfrom that.The result on linux should be this:
And if I wanted, I think I could use
curlto fetch the content of the file and then pipe it toenvsubst. So technically I don't even need the template in my filesystem.EDIT: Yes,
curlworks like a charm.Nice!