서버 및 환경/OS
[Linux] 특정 파일의 변수 리스트를 환경변수로 설정하기
그냥하Jo.
2022. 11. 13. 17:50
문제
변수명과 변수값이 각각 지정된 .env 파일이 있다고 하자.
# .env file
ID=smart-hinj
PWD=8191
export 명령어를 통해 변수 하나씩 환경변수로 선언할 수 있겠지만,
.env 파일 내 변수의 수가 많아지면 변수 하나씩 export 하기에는 비효율적이다.
이 파일에 존재하는 변수 리스트를 환경변수로 바로 전달하는 방법은 없을까?
해결
아래 명령어를 통해 .env 파일 내 변수 리스트를 환경변수로 한번에 전달할 수 있다.
아래 명령어를 shell에서 실행하자.
set -a # automatically export all variables
source .env
set +a
참조
https://stackoverflow.com/questions/43267413/shell-how-to-set-environment-variables-from-env-file
Shell how to set environment variables from .env file
Let's say I have .env file contains lines like below: USERNAME=ABC PASSWORD=PASS Unlike the normal ones have export prefix so I cannot source the file directly. What's the easiest way to create a
stackoverflow.com