bash에서 느낌표를 올바르게 벗어나는 방법은 무엇입니까? found 성가신 느낌표. “탈출하자”고 생각했습니다! 나는 한가지 여분의

오늘, 나는 트위터의 암호 생성기를 골프로 코딩하려고 시도하는 동안 손에 잡혔다.

import string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))

90 자 그것은 여분의 공간이 많기 때문에 막대를 올리고 실행 가능하게 만들기로 결정했습니다.

echo -e "#!/usr/bin/python\nimport string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))">pg;chmod +x pg;./pg

139 자 느낌표에서 분명히 배쉬 질식을 제외하고는 좋습니다.

badp@delta:~$ echo -e "#!/usr/bin/python\nimport string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))">pg;chmod +x pg;./pg
bash: !/usr/bin/python\nimport: event not found

성가신 느낌표. “탈출하자”고 생각했습니다! 나는 한가지 여분의 캐릭터를 가지고 있습니다.

echo -e "#\!/usr/bin/python\nimport string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))">pg;chmod +x pg;./pg

명백하게…

badp@delta:~$ echo -e "#\!/usr/bin/python\nimport string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))">pg;chmod +x pg;./pg
./pg: line 2: syntax error near unexpected token `('
./pg: line 2: `import string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))'
badp@delta:~$ cat pg
#\!/usr/bin/python
import string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))

내 asinine 코드 골프를 제쳐두고-나는 이것을 설명 할 수 없다.

으로 \!, 느낌표가 탈출했다가 있기 때문에, 정말없는 것을 제외 \!에 대해있는 그대로 방치 echo데리러.

하나의 솔루션이 \x21대신 사용되었을 수 있지만 bash 명령에서 느낌표를 이스케이프 처리하는 올바른 방법이라고 확신하지 않습니다.

tl; dr : bash 명령에서 느낌표를 어떻게 올바르게 탈출합니까?



답변

작은 따옴표를 사용하십시오.

echo -e '#!/usr/bin/python\nimport string as s,random;print "".join(random.sample(s.letters+s.digits+s.punctuation,9))'>pg;chmod +x pg;./pg

에 대한 규칙은 !나중에 다른 인용 규칙에 접붙였습니다 (csh에서). 쉘에 커맨드 라인 편집 기능이 없었을 때 매우 유용했지만 여전히 일부 사람들이 사용합니다.

추신 당신은 bash를 코딩하고 있기 때문에 :

echo $'#!/usr/bin/python\nimport string as s,random;print"".join(random.sample(s.letters+s.digits+s.punctuation,9))'>pg;chmod +x pg;./pg

이것은 대부분의 유니스에서 작동합니다.

echo python -c \''import string as s,random;print"".join(random.sample(s.letters+s.digits+s.punctuation,9))'\'>pg;chmod +x pg;./pg

(스크립트를 작성하려는 이유 또는 스크립트 이름이 두 글자 여야하는 이유를 이해하지 못했습니다.)


답변

요청하기 전에 Google 검색을해야합니다.

변수 [..]를 확장하기 위해 bash에 의존하지 않기 때문에 작은 따옴표를 대신 사용할 수 있습니다. 작은 따옴표로 묶인 문자열은 bash로 확장되지 않습니다.

경적응답내가 느낌표를 탈출 어떻게해야합니까?