.env 文件是用什么语言编写的?

mar*_*aft 15 environment-variables

按照惯例,.env文件是用什么语言或语法编写的?

它是sh脚本、bash脚本、JavaScript,还是受语法启发的精简sh语法?是否所有变量都定义了字符串,或者是否支持其他变量类型?

我最近开始.env在我的 NodeJS 应用程序中使用文件,但我在文档中找不到它是什么语言或我必须遵循的语法约束。我在docker docs 中找到了一个定义,这似乎表明MY_VAR=my val它本身就是唯一值得注意的特性。

编辑:

虽然我在 NodeJS 的上下文中遇到了这个问题,但这个问题并非特定于 nodeJS。 .env应考虑在其他上下文中使用的文件。

Ami*_*mir 13

假设您指的是 npm dotenv 包如何解释 .env 文件:

该文件仅用作“文本”配置,由模块解析。解析后的配置作为添加环境​​变量的基础。所以 env 文件本身并不是真正用任何编程语言编写的。

解析规则可以在这里找到:https : //www.npmjs.com/package/dotenv#rules

解析代码可以在这里找到:https : //github.com/motdotla/dotenv/blob/master/lib/main.js

  • 谢谢,这很有帮助。它使用正则表达式 `/^\s*([\w.-]+)\s*=\s*(.*)?\s*$/` 抓取键值对,然后修剪该值除其他事项外。该值始终是一个字符串。 (3认同)

yzo*_*org 9

Here are the rules, copy-pasted from link above. Feel free to update anytime ("Commnity wiki").


Rules

The parsing engine currently supports the following rules:

  • BASIC=basic becomes {BASIC: 'basic'}

  • empty lines are skipped

  • lines beginning with # are treated as comments

  • empty values become empty strings (EMPTY= becomes {EMPTY: ''})

  • inner quotes are maintained (think JSON) (JSON={"foo": "bar"} becomes {JSON:"{\"foo\": \"bar\"}")

  • whitespace is removed from both ends of unquoted values (see more on trim) (FOO= some value becomes {FOO: 'some value'})

  • single and double quoted values are escaped (SINGLE_QUOTE='quoted' becomes {SINGLE_QUOTE: "quoted"})

  • single and double quoted values maintain whitespace from both ends (FOO=" some value " becomes {FOO: ' some value '})

  • double quoted values expand new lines, MULTILINE="new\nline" becomes

    {MULTILINE: 'new
    line'}
    
    Run Code Online (Sandbox Code Playgroud)