英文:
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 << 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"
答案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
- The above construct is called Here Documents
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=".env"
cat >> "$env_file" <<'EOF'
PIN=1
DEVICE_PREFIX=xxx
DEV=true
EOF
- The above construct is called Here Documents
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>
creates it if it does not exists. -
See Redirections
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论