Copy text to local .env file with bash.

huangapple go评论55阅读模式
英文:

How to copy text to local .env file with bash

问题

Sure, here's the translation of your text:

有没有办法在shell脚本中将文本复制到本地的.env文件中,以便我可以自动设置这些环境变量?

例如,我想将这些内容复制到与我已经cd到的同一目录中的.env文件中:

PIN=1
DEVICE_PREFIX=xxx
DEV=true
英文:

Is there a way in a shell script to copy text into a local .env file so that I can automate setting those environment variables?

example, I want to copy this to an .env file that sits in the same directory I'm already cd'd to

PIN=1
DEVICE_PREFIX=xxx
DEV=true

答案1

得分: 1

这是您要翻译的内容:

有多种方法,其中一个我没有看到的是最简单的:

echo "
PIN=1
DEVICE_PREFIX=xxx
DEV=true
" > .env

使用 echo 可以进行多行打印,然后将输出重定向到文件,即 .env

希望对您有所帮助。

英文:

Well there are multiple approaches, one that I don't see here is the most simplest

echo "
PIN=1
DEVICE_PREFIX=xxx
DEV=true
" > .env

with echo can do multi line printing and then redirect the output to the file i.e .env

Hope it helps

答案2

得分: 0

#!/bin/bash

text=$(cat << EOF
PIN=1
DEVICE_PREFIX=xxx
DEV=true
EOF
)

# Specify the path to the .env file
env_file=".env"

# Copy the text to the .env file
echo "$text" > "$env_file"
英文:
#!/bin/bash

text=$(cat &lt;&lt; EOF
PIN=1
DEVICE_PREFIX=xxx
DEV=true
EOF
)

# Specify the path to the .env file
env_file=&quot;.env&quot;

# Copy the text to the .env file
echo &quot;$text&quot; &gt; &quot;$env_file&quot;

答案3

得分: 0

An alternative without creating/saving the data/input to a variable, something like:

#!/usr/bin/env sh

env_file=".env"

cat >> "$env_file" <<'EOF'
PIN=1
DEVICE_PREFIX=xxx
DEV=true
EOF

A note on the redirections (> and >>) from the shell.

  • The >> appends to the existing non-empty file.

  • The > Replaces the data/contents of an existing non-empty file.

  • A word of caution, the > truncates an existing file, but both >> and > create it if it does not exist.

  • See Redirections

英文:

An alternative without creating/saving the data/input to a variable, something like:

#!/usr/bin/env sh

env_file=&quot;.env&quot;

cat &gt;&gt; &quot;$env_file&quot; &lt;&lt;&#39;EOF&#39;
PIN=1
DEVICE_PREFIX=xxx
DEV=true
EOF

A note on the redirections ( > and >> ) from the shell.

  • The &gt;&gt; appends to the existing non-empty file.

  • The &gt; Replaces the data/contents of an existing non-empty file.

  • A word of caution, the &gt; truncates an existing file, but both &gt;&gt; and &gt; creates it if it does not exists.

  • See Redirections

huangapple
  • 本文由 发表于 2023年5月17日 10:32:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定