환경 변수에 사용되는 문자는 케이스마다 다르다
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"
왜 다릅니까
근거선인의 지혜
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 "herezero=aaa" /bin/bash -c 'env | grep zero | od -a'
0000000 h e r e ? 80 8b z e r o = a a a nl
Reference
이 문제에 관하여(환경 변수에 사용되는 문자는 케이스마다 다르다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://zenn.dev/notrogue/articles/81e41153bfa42d텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)