Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What language is a .env file written in?

By convention, what language or syntax is a .env file written in?

Is it a sh script, a bash script, JavaScript, or is it a stripped down no-frills syntax inspired by sh syntax? Are all variables defined strings, or are other variable types supported?

I recently started using .env files in my NodeJS applications, but I can find nowhere in the documentation what language it is in or what constraints on syntax I must follow. I found a definition in the docker docs which seems to suggest MY_VAR=my val is the only notable feature, per se.

EDIT:

While I encountered this in the context of NodeJS, the question is not specific to nodeJS. .env files used in other contexts should be considered.

like image 572
mareoraft Avatar asked Jun 28 '19 13:06

mareoraft


People also ask

What is .env file format?

A . env file or dotenv file is a simple text configuration file for controlling your Applications environment constants. Between Local, Staging and Production environments, the majority of your Application will not change.

What is .env file in Python?

A . env file is a text file containing key value pairs of all the environment variables required by your application. This file is included with your project locally but not saved to source control so that you aren't putting potentially sensitive information at risk.

How do I make an .env file or code?

Once you have opened the folder, click on the Explorer icon on the top left corner of the VSCode (or press Ctrl+Shift+E) to open the explorer panel. In the explorer panel, click on the New File button as shown in the following screenshot: Then simply type in the new file name . env ...

What is .env file in PHP?

An . env file is a plain text file which contains environment variables definitions which are designed so your PHP application will parse them, bypassing the Apache, NGINX and PHP-FPM. The usage of . env files is popular in many PHP frameworks such as Laravel which has built-in support for parsing .


2 Answers

Assuming you're referring to how the .env file is interpreted by the npm dotenv package:

The file simply serves as a 'text' configuration, which is parsed by the module. The parsed configuration is used as a basis for adding environment variables. So the env file itself is not really written in any programming language.

Parsing rules can be found here: https://www.npmjs.com/package/dotenv#rules

Parsing code can be found here: https://github.com/motdotla/dotenv/blob/master/lib/main.js

like image 77
Amir Avatar answered Oct 22 '22 09:10

Amir


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'}
    
like image 28
yzorg Avatar answered Oct 22 '22 11:10

yzorg