GrepBoard/매뉴얼
605 편 ·

/bin/bash^M: bad interpreter 해결 (CRLF)

·시험 환경 Ubuntu 24.04 · bash 5.2.21 · Rocky 9.3 · bash 5.1.8 (Docker) · 2026-10·시험 범위 ↓·5 단계

bad interpreter 는 줄 끝이 CRLF 라서 납니다(Ubuntu 24.04 는 required file not found). sed 로 \r 을 지우고 .gitattributes 로 막습니다.

output
bash: ./deploy.sh: /bin/bash^M: bad interpreter: No such file or directory

스크립트의 줄 끝이 Windows 식 CRLF(\r\n)라서, 첫 줄 #!/bin/bash 뒤에 붙은 \r(^M)까지 인터프리터 이름으로 읽힌 것입니다. /bin/bash\r 라는 파일은 없으니 실행이 실패합니다. 줄 끝의 \r 을 지우면 바로 풀리고, 다시 생기지 않게 하려면 저장소에서 줄 끝을 LF 로 고정합니다.

같은 파일이라도 bash 판에 따라 문구가 다릅니다. Ubuntu 24.04(bash 5.2.21)는 ^M 을 보여 주지 않아 원인을 짐작하기 어렵습니다.

실행한 곳문구
Rocky 9 · bash 5.1.8bash: ./deploy.sh: /bin/bash^M: bad interpreter: No such file or directory
Ubuntu 24.04 · bash 5.2.21bash: line 1: ./deploy.sh: cannot execute: required file not found
Ubuntu 24.04 · sh(dash)sh: 1: ./deploy.sh: not found
Docker ENTRYPOINTexec /entry.sh: no such file or directory

파일이 분명히 있는데 「not found」가 나오면 이 글의 원인을 먼저 의심합니다.

[1] 줄 끝을 확인합니다. file 이 with CRLF line terminators 라고 알려 줍니다.

console
$ file deploy.shdeploy.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators

cat -A 로 보면 줄마다 끝에 ^M$ 가 붙어 있습니다. $ 가 줄 끝이고 그 앞 ^M 이 \r 입니다.

console
$ cat -A deploy.sh#!/bin/bash^M$echo "deploy start"^M$cd /tmp^M$echo done^M$

[2] bash deploy.sh 로 우회하지 않습니다. 첫 줄을 건너뛰니 실행은 되지만, 나머지 줄의 \r 이 인자에 섞여 엉뚱하게 실패합니다.

console
$ bash deploy.shdeploy startdonedeploy.sh: line 3: cd: $'/tmp\r': No such file or directory

cd 는 실패했는데 종료 코드는 0 이었습니다(마지막 echo 가 성공해서). 스크립트가 엉뚱한 디렉터리에서 다음 명령을 실행할 수 있으니 반드시 파일을 고칩니다.

[3] \r 을 지웁니다. 추가 설치 없이 되는 방법입니다. 원본을 남기려면 먼저 복사합니다.

console
$ cp deploy.sh deploy.sh.bak$ sed -i 's/\r$//' deploy.sh$ file deploy.shdeploy.sh: Bourne-Again shell script, ASCII text executable$ ./deploy.shdeploy startdone

dos2unix 가 있으면 그것도 됩니다(Ubuntu 는 dos2unix 패키지).

console
$ dos2unix deploy.shdos2unix: converting file deploy.sh to Unix format...

[4] 같은 문제가 있는 파일을 한꺼번에 찾습니다. -I 는 바이너리 파일을 건너뜁니다.

console
$ grep -rlI $'\r' --include='*.sh' /srv/app/srv/app/deploy.sh/srv/app/other.sh

찾은 파일만 [3] 으로 고칩니다. *.bat·*.ps1 처럼 Windows 에서 쓰는 파일은 CRLF 가 맞으므로 범위를 셸 스크립트로 좁힙니다.

[5] 저장소에서 다시 생기지 않게 합니다. 대부분 Windows 에서 편집하거나 Windows 에서 체크아웃해 서버로 복사할 때 생깁니다. Git 공식 문서의 예처럼 .gitattributes 에 셸 스크립트의 줄 끝을 LF 로 고정합니다.

.gitattributes
*.sh    text eol=lf

eol=lf 는 「체크아웃할 때 작업 디렉터리에 이 줄 끝을 쓴다」는 뜻이고, eol 을 주면 text 도 함께 켜집니다. 이 파일을 넣기 전에 이미 CRLF 로 커밋된 파일은 그대로 남습니다. 문서가 안내하는 정규화 절차(git add --renormalize . 후 git status 로 확인하고 커밋)를 한 번 거쳐야 기존 파일도 바뀝니다.

컨테이너 이미지 안에 들어간 스크립트라면 이미지를 다시 빌드해야 고친 파일이 들어갑니다. 진입점 스크립트가 exec format error 로 실패하면 첫 줄 셔뱅이 아예 없는 경우이니 Docker exec format error 를 봅니다.

#bash#crlf#line-endings#dos2unix