TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Dotenv, if it is a Unix utility

225 点作者 gyf304大约 1 年前
I like the idea of using dotenv files, but I dislike having to use different language-specific libraries to read them.<p>To solve this, I created a small utility that lets you prefix any command with &quot;dotenv&quot; to load the &quot;.env&quot; file.<p>This is how I imagine dotenv would work if it had started as a UNIX utility rather than a Node.js library.

19 条评论

petepete大约 1 年前
I think direnv already does a good job in this space, and it&#x27;s already available in your package manager.<p><a href="https:&#x2F;&#x2F;direnv.net&#x2F;" rel="nofollow">https:&#x2F;&#x2F;direnv.net&#x2F;</a>
评论 #40192798 未加载
评论 #40192340 未加载
评论 #40193755 未加载
评论 #40195254 未加载
评论 #40201298 未加载
tuyiown大约 1 年前
Please just see this<p>`env -S &quot;$(cat .env)&quot; &lt;cmd&gt;`<p>Believe it or not that’s all you need.<p>&gt; S, --split-string=S process and split S into separate arguments; used to pass multiple arguments on shebang lines<p><i>edit: forgot the quotes around shell substitution</i>
评论 #40195460 未加载
评论 #40195413 未加载
评论 #40195680 未加载
评论 #40195840 未加载
评论 #40196942 未加载
评论 #40196138 未加载
评论 #40195849 未加载
grounder大约 1 年前
Compare with dotenvx - <a href="https:&#x2F;&#x2F;github.com&#x2F;dotenvx&#x2F;dotenvx">https:&#x2F;&#x2F;github.com&#x2F;dotenvx&#x2F;dotenvx</a> This is my current tool of choice.
kazinator大约 1 年前
I just remembered. Adding a -f &lt;file&gt; option to the GNU Coreutils env utility has previously been discussed:<p><a href="https:&#x2F;&#x2F;lists.gnu.org&#x2F;archive&#x2F;html&#x2F;coreutils&#x2F;2021-10&#x2F;msg00001.html" rel="nofollow">https:&#x2F;&#x2F;lists.gnu.org&#x2F;archive&#x2F;html&#x2F;coreutils&#x2F;2021-10&#x2F;msg0000...</a><p>It came up in the mailing also this March. I saw the posting in my inbox and proposed that a null-terminated format be handled, which is exactly like &#x2F;proc&#x2F;&lt;pid&gt;&#x2F;env:<p><a href="https:&#x2F;&#x2F;lists.gnu.org&#x2F;archive&#x2F;html&#x2F;coreutils&#x2F;2024-03&#x2F;msg00140.html" rel="nofollow">https:&#x2F;&#x2F;lists.gnu.org&#x2F;archive&#x2F;html&#x2F;coreutils&#x2F;2024-03&#x2F;msg0014...</a><p>If that feature were available, this becomes<p><pre><code> env -f .env command arg ...</code></pre>
miohtama大约 1 年前
There is also shdotenv that allows you to load different .env file formats and convert between them, e.g. for UNIX shell.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;ko1nksm&#x2F;shdotenv">https:&#x2F;&#x2F;github.com&#x2F;ko1nksm&#x2F;shdotenv</a>
评论 #40193158 未加载
评论 #40195322 未加载
tester457大约 1 年前
dotenv started as a ruby library actually. The first implementation inspired the others such as the golang version of the library.
whalesalad大约 1 年前
<p><pre><code> export $(cat .env | xargs) </code></pre> Agree with the premise but this can be achieved with actual Unix concepts no need for anything else.<p>The language runtime dotenv projects are banned in my engineering org.
评论 #40193815 未加载
评论 #40193993 未加载
评论 #40195202 未加载
评论 #40193713 未加载
mongol大约 1 年前
Would not<p>sh -c &#x27;. .env; echo $MY_VAR&#x27;<p>do the same thing? (I am not in front of a shell at the moment.)
评论 #40193444 未加载
评论 #40192375 未加载
评论 #40192372 未加载
评论 #40192810 未加载
评论 #40192354 未加载
iDon大约 1 年前
Thanks to OP and other posters - various ideas useful in different cases.<p>The xargs idea made me think of using bash as the parser :<p><pre><code> bash -c &quot;exec -c bash -c &#x27;source $CONFIG&#x2F;main.bash; env&#x27;&quot;</code></pre> This test .bash file contains multiple source-s of other .bash files, which contain a mix of comments, functions, set and env vars - just the env vars are exported by env. This seems useful e.g. for collating &amp; summarising an environment for docker run -e.<p>This outputs the env vars to stdout; for the OP&#x27;s purpose, the output could be sourced :<p><pre><code> envFile=$(mktemp &#x2F;tmp&#x2F;env.XXXXXX); bash -c &quot;exec -c bash -c &#x27;source $CONFIG&#x2F;main.bash; env&#x27;&quot; &gt; $envFile; env $(cat $envFile) sh -c &#x27;echo $API_HOST&#x27; </code></pre> # For Bourne shell, use env -i in place of exec -c :<p>sh -c &quot;env -i sh -c &#x27;. $CONFIG&#x2F;main.sh; env&#x27;&quot; &gt; $envFile
andy_ppp大约 1 年前
This looks good and neater than my solution in my .zshrc:<p>envup() {<p><pre><code> local file=$([ -z &quot;$1&quot; ] &amp;&amp; echo &quot;.env&quot; || echo &quot;.env.$1&quot;) if [ -f $file ]; then set -a source $file set +a else echo &quot;No $file file found&quot; 1&gt;&amp;2 return 1 fi</code></pre> }<p>You can also specify `envup development` to load .env.development files should you want. Obviously this will pollute the current shell but for me it is fine.
belthesar大约 1 年前
This is interestingly similar to a little tool I wrote called sops-run [0], which manages encrypted secrets for cli tools using Mozilla’s sops [1]. Biggest upshot is that you can use it more confidently for secrets with encryption at rest. Built it when I was trying out CLI tools that wanted API keys, but I didn’t want to shove them into my profile and accidentally upload them into my dotfiles repository. Do need to finally get back to making this a package, being able to install it with pip(x) would be really nice.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;belthesar&#x2F;sops-run">https:&#x2F;&#x2F;github.com&#x2F;belthesar&#x2F;sops-run</a><p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;getsops&#x2F;sops">https:&#x2F;&#x2F;github.com&#x2F;getsops&#x2F;sops</a>
vishvananda大约 1 年前
Doesn’t this already exist as <a href="https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;dotenv-cli" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;dotenv-cli</a> ?
supriyo-biswas大约 1 年前
I wrote my own some time ago: <a href="https:&#x2F;&#x2F;github.com&#x2F;supriyo-biswas&#x2F;dotfiles&#x2F;commit&#x2F;39585b42c24ae2d18112d142cf6081354eb29c77">https:&#x2F;&#x2F;github.com&#x2F;supriyo-biswas&#x2F;dotfiles&#x2F;commit&#x2F;39585b42c2...</a>
wodenokoto大约 1 年前
Since loading dotenv files happens together with executing code I I have decided to trust my .env files just like I trust the rest of my code not to delete my entire system and therefore I source them.
KevinMS大约 1 年前
I never understood why it had to be a dot file, except for naming it.
fire_lake大约 1 年前
Does this accept the exact same format (including quotes and whitespace) as a Docker env file? That’s a key feature for me
prmoustache大约 1 年前
I don&#x27;t understand what more it does than sourcing a file on your shell would? Anyone can explain?
评论 #40195336 未加载
kzrdude大约 1 年前
This idea seems to be cloned everywhere now, so something is causing the popularity
评论 #40193313 未加载
bitwize大约 1 年前
C? Y u no Rust?
评论 #40195106 未加载