@@ -543,6 +543,62 @@ In addition to your own env vars, this ``.env`` file also contains the env vars
543
543
defined by the third-party packages installed in your application (they are
544
544
added automatically by :ref: `Symfony Flex <symfony-flex >` when installing packages).
545
545
546
+ .env File Syntax
547
+ ................
548
+
549
+ Add comments by prefixing them with ``# ``:
550
+
551
+ .. code-block :: bash
552
+
553
+ # database credentials
554
+ DB_USER=root
555
+ DB_PASS=pass # this is the secret password
556
+
557
+ Use environment variables in values by prefixing variables with ``$ ``:
558
+
559
+ .. code-block :: bash
560
+
561
+ DB_USER=root
562
+ DB_PASS=${DB_USER} pass # include the user as a password prefix
563
+
564
+ .. caution ::
565
+
566
+ The order is important when some env var depends on the value of other env
567
+ vars. In the above example, ``DB_PASS `` must be defined after ``DB_USER ``.
568
+ Moreover, if you define multiple ``.env `` files and put ``DB_PASS `` first,
569
+ its value will depend on the ``DB_USER `` value defined in other files
570
+ instead of the value defined in this file.
571
+
572
+ Define a default value in case the environment variable is not set:
573
+
574
+ .. code-block :: bash
575
+
576
+ DB_USER=
577
+ DB_PASS=${DB_USER:- root} pass # results in DB_PASS=rootpass
578
+
579
+ .. versionadded :: 4.4
580
+
581
+ The support for default values has been introduced in Symfony 4.4.
582
+
583
+ Embed commands via ``$() `` (not supported on Windows):
584
+
585
+ .. code-block :: bash
586
+
587
+ START_TIME=$( date)
588
+
589
+ .. caution ::
590
+
591
+ Using ``$() `` might not work depending on your shell.
592
+
593
+ .. tip ::
594
+
595
+ As a ``.env `` file is a regular shell script, you can ``source `` it in
596
+ your own shell scripts:
597
+
598
+ .. code-block :: terminal
599
+
600
+ $ source .env
601
+
546
602
.. _configuration-multiple-env-files :
547
603
548
604
Overriding Environment Values via .env.local
0 commit comments