0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
JPPINTO
  • Home
  • Blog
  • Certifications
  • About
  • Contact
  • Shop
  • Gallery
  • Current Setup
Contact

Search

July 3, 2026 / Linux, Servers, Ubuntu

Fix Windows Line Endings and Make Shell Scripts Executable in Linux

Tags: bash, chmod, line endings, linux, sed, shell script, ubuntu
Featured image for Fix Windows Line Endings and Make Shell Scripts Executable in Linux

If you edit Linux shell scripts on Windows, you may run into confusing errors when you copy those scripts back to Ubuntu or another Linux server.

The script may look correct, but Linux can still fail to run it because Windows and Linux use different line endings. Windows commonly saves text files with CRLF line endings. Linux shell scripts should use LF line endings.

At the same time, copied scripts may not have execute permission. That means the file exists, but Linux will not run it as a program until you add chmod +x.

This article shows the common errors and a single command that fixes both problems for every .sh file in the current folder tree.

Common Errors

One common error is:

/bin/bash^M: bad interpreter: No such file or directory

The ^M means the file has Windows carriage-return characters at the end of each line.

You may also see:

bash: ./deploy.sh: Permission denied

That usually means the script does not have execute permission.

Another possible error is:

./deploy.sh: line 2: $'\r': command not found

This also points to Windows line endings inside the script.

Why This Happens

Linux and Windows store line breaks differently:

  • Linux uses LF.
  • Windows often uses CRLF.

The extra Windows carriage-return character is invisible in many editors, but Bash can still read it. When Bash sees that hidden character in places like the shebang line, commands, or variable assignments, the script can fail.

For example, this first line may look normal:

#!/bin/bash

But if the file has Windows line endings, Linux may read it like:

#!/bin/bash^M

Linux then tries to find an interpreter named /bin/bash^M, which does not exist.

Check the Script

Move into the folder that contains your scripts:

cd /srv/admin/scripts

List the shell scripts:

find . -type f -name "*.sh" -print

Example output:

./deploy-site.sh
./inventory/check-server.sh
./maintenance/backup-database.sh

If one of these files was edited on Windows, it may need its line endings fixed.

Fix Line Endings and Make Scripts Executable

Run this command from the folder where you want to repair shell scripts:

find . -type f -name "*.sh" -print -exec sed -i 's/\r$//' {} \; -exec chmod +x {} \;

What the command does:

  • find . starts in the current folder.
  • -type f only matches files.
  • -name "*.sh" only matches shell scripts ending in .sh.
  • -print shows each file that is being changed.
  • sed -i 's/\r$//' removes Windows carriage-return characters from the end of each line.
  • chmod +x makes each script executable.

Example output:

./deploy-site.sh
./inventory/check-server.sh
./maintenance/backup-database.sh

After this runs, each .sh file should have Linux line endings and execute permission.

Test a Script

Run one of the scripts again:

./deploy-site.sh

If you still get a permission error, check the permissions:

ls -l deploy-site.sh

You should see x in the permissions, like this:

-rwxrwxr-x 1 admin admin 2401 Jul  2 22:18 deploy-site.sh

The x means the file is executable.

Run the Fix Safely

Be careful where you run the command. It works recursively from the current folder downward.

Before changing files, you can preview which .sh files will be touched:

find . -type f -name "*.sh" -print

If the list looks correct, run the full fix:

find . -type f -name "*.sh" -print -exec sed -i 's/\r$//' {} \; -exec chmod +x {} \;

If you only want to fix one folder, change into that folder first:

cd /srv/admin/scripts/site-maintenance
find . -type f -name "*.sh" -print -exec sed -i 's/\r$//' {} \; -exec chmod +x {} \;

Fix One Script Only

If you want to fix a single file instead of every .sh file, run:

sed -i 's/\r$//' deploy-site.sh
chmod +x deploy-site.sh

Then run it:

./deploy-site.sh

Prevent the Problem

Many editors can save files with Linux line endings.

In Visual Studio Code, check the lower-right corner of the window. If it says CRLF, click it and change it to LF, then save the file.

If your team stores scripts in Git, you can also use a .gitattributes file to keep shell scripts as Linux-style text:

*.sh text eol=lf

That helps prevent Windows edits from reintroducing CRLF line endings into shell scripts.

Quick Reference

Preview affected scripts:

find . -type f -name "*.sh" -print

Fix all .sh files below the current folder and make them executable:

find . -type f -name "*.sh" -print -exec sed -i 's/\r$//' {} \; -exec chmod +x {} \;

Fix one script:

sed -i 's/\r$//' deploy-site.sh
chmod +x deploy-site.sh

This is a simple cleanup step, but it fixes two of the most common problems with shell scripts that were edited on Windows: hidden CRLF characters and missing execute permission.

Post Views: 28
<- How to Use Alias in Linux and Ubuntu
Install and Tune Nginx on Ubuntu for Web Hosting ->

Categories

  • Active Directory (5)
  • AI (2)
  • Amazon Cloud Services (1)
  • Blazor (1)
  • C# (C-Sharp) (3)
  • CI/CD Pipelines (1)
  • Containers (4)
  • Deployment (2)
  • Development (4)
  • Docker (3)
  • General (5)
  • IIS 6.0 (4)
  • IIS 7.0 (10)
  • IIS 8.0 (1)
  • Infrastructure as Code (IaC) (1)
  • Kubernetes (3)
  • Linux (8)
  • Microsoft 365 (2)
  • MySQL (1)
  • Office 2010 (1)
  • PHP (1)
  • PowerShell (7)
  • Productivity (1)
  • Servers (8)
  • SharePoint 2007 (8)
  • SharePoint 2010 (19)
  • SharePoint 2013 (2)
  • SharePoint Online (1)
  • SMTP (4)
  • SQL Server 2008 (1)
  • SQL Server 2008 R2 (1)
  • SQL Server 2012 (2)
  • SQL Server 2019 (1)
  • Ubuntu (8)
  • Uncategorized (1)
  • URL Rewrite (2)
  • Visual Studio 2019 (1)
  • Visual Studio Code (1)
  • Windows 10 (5)
  • Windows 2003 (9)
  • Windows 2008 (18)
  • Windows 2012 (6)
  • Windows 7 (3)
  • Windows Firewall (1)
  • Windows Vista (1)
  • WordPress (3)
  • WP-CLI (3)

Recent Posts

  • Create an Nginx Default Catch-All Site on Ubuntu
  • Install and Configure Redis on Ubuntu for Local Object Cache
  • Install and Configure MySQL on Ubuntu for WordPress
  • Install PHP-FPM and Common PHP Extensions on Ubuntu
  • Install and Tune Nginx on Ubuntu for Web Hosting

Advertisement

Tags

ai coding agents bash developer workflow dev to production exe permissions externalize blob externalize sharepoint data filezilla server firewall rules filazilla full installation http redirect https https redirect IIS iis7 iis 7 installation IIS installation index server configuration installing cumulative updates linux load balance central administration microsoft 365 nginx nlb powerpoint powershell redirect http to https server setup sharepoint 2010 cumulative updates sharepoint 2010 farm build sharepoint 2010 farm configuration sharepoint 2010 farm installation sharepoint data externalization SMTP storagepoint ubuntu web server windows Windows 7 windows firewall configuration windows server 2008 wlbs wordpress wp-cli x86
© 2026 JPPinto.com. All rights reserved.