환경 변수에 사용되는 문자는 케이스마다 다르다

4156 단어 LinuxshellmacOStech

tl;dr

  • 환경 변수 중 대문자, 수치, 밑줄 이외의 문자를 사용하지 않는 것이 좋다
  • 단, 이외의 문자도 조개껍질에 따라 사용할 수 있다
  • 시험해 보다


    조개 몇 개로 해볼게요.마지막 ash 이외에는 맥OS에서 실행됩니다.
    # dashからだと見られない
    bash-3.2$  env "hyphen-var=aaaaaaaaaaaaaab" /bin/dash -c 'env | grep hyphen'
    # bashだと見られる
    bash-3.2$  env "hyphen-var=aaaaaaaaaaaaaab" /bin/bash -c 'env | grep hyphen'
    hyphen-var=aaaaaaaaaaaaaab
    # zshからは見られない
    env "hyphen-var=aaaaaaaaaaaaaab" /bin/zsh -c 'env | grep hyphen'
    # fishからは見られる
    env "hyphen-var=aaaaaaaaaaaaaab" /usr/local/bin/fish -c 'env | grep hyphen'
    hyphen-var=aaaaaaaaaaaaaab
    # sh(POSIX互換モードのbash?)からは見られる
    bash-3.2$  env "hyphen-var=aaaaaaaaaaaaaab" /bin/sh -c 'env | grep hyphen'
    hyphen-var=aaaaaaaaaaaaaab
    # ashからは見られる
    docker run -e "hyphen-var=aaaaaaaaaaaaaab" -it --rm alpine:3.15.0   /bin/ash -c 'env | grep hyphen'                                                         972ms  日 12/ 5 18:06:26 2021
    hyphen-var=aaaaaaaaaaaaaab
    
    # bash+declareからでも見られる
    env "hyphen-var=aaaaaaaaaaaaaab" /bin/bash -c 'declare -x | grep hyphen'                                                                                                日 12/ 5 18:09:14 2021
    declare -x hyphen-var="aaaaaaaaaaaaaab"
    

    왜 다릅니까


    근거선인의 지혜
  • POSIX에서 환경 변수는 대문자, 숫자 및 밑줄에서 시작되지 않은 값의 이름을 지원해야 합니다.

  • Environment variable names used by the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit.
  • 하이픈이 포함된 환경 변수 이름은 지원되지 않을 수 있습니다.그러나 다른 문자를 지원할 수 있습니다

  • Other characters may be permitted by an implementation;
  • 그런 것 같아요.

    조개껍질이 하나 있는데, 너는 왜 특별히 사용하지 않느냐


    일부러 하이픈을 제거하는 것이 더 편리하지 않습니까?하지만 Stackexchange의 답변에 따르면 안전성Shellshcok의 영향에 신경을 많이 쓴 것 같다.
    Note that some shells (e.g. modern dash, mksh, zsh) remove variables whose name they don't like from the environment. (Shellshock has caused people to be more cautious about environment variable names, so restrictions are likely to become tighter over time, not more permissive.)
    예를 들어dash구 버전은 2012년에 하이픈을 사용할 수 없었다는 구 버전2010년 출시된 Ubuntu lucid의dash로 테스트하면 하이픈을 사용할 수 있다.
    # 古いバージョン(2010年リリース)のUbuntuのdashでは使える
    docker run -e "hyphen-var=aaaaaaaaaaaaaab" -it --rm ubuntu:lucid   /bin/dash -c 'env | grep hyphen'                                           
    hyphen-var=aaaaaaaaaaaaaab
    
    # 新しいバージョン(2021年リリース)のUbuntuのdashでは使えない
    docker run -e "hyphen-var=aaaaaaaaaaaaaab" -it --rm ubuntu:jammy   /bin/dash -c 'env | grep hyphen'
    

    추가 문자


    그림 문자의 환경 변수 이름 (적어도 bash에서) 도 사용할 수 있습니다.
    env "😺=aaaaaaaaaaaaaab" /bin/bash -c 'env | grep 😺'
    😺=aaaaaaaaaaaaaab
    
    영폭 공간도 갈 수 있다(here와zero 사이에 존재한다).
     env "here​zero=aaa" /bin/bash -c 'env | grep zero | od -a'                 
    0000000    h   e   r   e   ?  80  8b   z   e   r   o   =   a   a   a  nl
    

    좋은 웹페이지 즐겨찾기