오늘의 인기 글
최근 글
최근 댓글
Today
Total
05-02 05:05
관리 메뉴

우노

[Shell Script] 환경 변수 선언 (export) 및 호출 (os.environ) 본문

Operating System/Shell Script

[Shell Script] 환경 변수 선언 (export) 및 호출 (os.environ)

운호(Noah) 2020. 12. 19. 22:50

export

  • 변수 명 앞에 export을 붙여주면 환경 변수(environment variable)로 설정되어
  • Python 파일, 자식 스크립트 등에서 해당 변수 사용이 가능해진다.

예제 1 - Python 파일에서 환경 변수 선언 및 호출

#!/bin/bash

# python 코드 안에서 사용할 수 있는 환경 변수 선언
export name="unho"

# EOF 내의 명령어는 python3로 수행됨
python3 - << 'EOF'

# 모듈 import
import os

# 환경 변수 호출
print(os.environ["name"])

EOF

예제 2 - 자식 스크립트에서 환경 변수 선언 및 호출

  • 부모 쉘 스크립트(parent.sh)

      #!/bin/bash
    
      # 환경변수 선언
      export parent="iamparent"
    
      # 자식 쉘 스크립트 실행
      sh /Users/bdlab/Desktop/child.sh
  • 자식 쉘 스크립트(child.sh)

      #!/bin/bash
    
      echo ${parent}
  • 부모 쉘 스크립트(parent.sh) 실행

      ./parent.sh
      # iamparent
Comments