I have a Python project which is a bunch of Docker containers and terraform scripts. My workstation is Windows 10 so I have installed Ubuntu in WSL to develop it. The problem I am facing is there are a lot of shell scripts (.sh) that I need to run as the build process. In Git it always checks out the files as Windows style and commit files as Unix Style. As a result, the Ubuntu bash shell does not like these shell scripts and I have to run dos2unix before running a file. Is there a one liner that allows me to run these scripts without modifying them?
-
Can you fix up whatever git client you are using to preserve line-ends on checkout? One easier option might be to use git on WSL command line (it's what I do). – JNevill Mar 11 '20 at 14:26
3 Answers
If the scripts don't call another script you could
tr "\r\n" "\n" script.sh | bash
But in the other case I think you need to change your repository config to tell git to don't convert files with .sh extension. Look at this answer https://stackoverflow.com/a/39461324/5032541
If you want to change git configuration to remove auto conversion, take a look on official documentation. But it's a bad idea.

- 1,319
- 11
- 26
The main problem imo is checking in the wrong file endings into git.
An easy way to fix all shell scripts at once is:
find . -type f -name "*.sh" -exec dos2unix {} \;
It will recursively find all files with the .sh extension and perform dos2unix on it.

- 1,026
- 9
- 29
I would recommend setting your git repo to checkout linux style rather than windows line endings.
I do a lot of node/docker/linux work but have a windows 10 laptop, this is what I have done.
git config --global core.autocrlf input
See this link for some more details. This way you dont need to continuously run scripts to make sure you have the correct line endings. Also a good thing to do is set you editor to default to linux line endings.

- 5,698
- 3
- 37
- 55