Answer the question
In order to leave comments, you need to log in
How to edit .env file most natively?
There is a .env file, it stores some values in the usual format for this file:
ENV_VAR_1=123
ENV_VAR_2=456
ENV_VAR_3=789
ENV_VAR_1=321 # изменено
ENV_VAR_2=456
ENV_VAR_3=789
Answer the question
In order to leave comments, you need to log in
What if the value of the variable is unknown?
sed and basic regex ownership is what you need.
Change a line, add, delete, right in the file.
Display filename on the screen with the changes made.
Actually save the changes made to the file itself.
Most often sed is used with the "s" (replacement) command, e.g.sed -ir "s/ENV_VAR_1=.*/ENV_VAR_1=321/" dev.env
Who will search - sketched a script
#!/bin/bash
name=$1
value=$2
envfile=$3
if [ "$envfile" == "" ]; then
envfile=~/.env
fi
if [ ! -e $envfile ]; then
touch $envfile
fi
have=$(grep $name $envfile)
if [ "$have" == "" ]; then
echo "$name=$value" >> $envfile
else
sed -ir "s/$name=.*/$name=$value/" $envfile
rm -f ${envfile}r
fi
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question