Bash File Unable to locate package when new line is added

When building a bash file to execute I keep getting an error after adding a new line. I have tried moving some lines around or removing some of the packages but it will results in the same error. In the file if I was to remove 3 and 4 it there will no longer be an error.

E: Unable to locate package <package>

The bash file looks like this

#!/usr/bin/env bash
sudo apt update && sudo apt install -y python3-pip build-essential python3-dev python3-setuptools gcc sshpass
sudo apt-get install apt-transport-https lsb-release software-properties-common dirmngr
sudo apt-get update

The output with the error

Hit:1 http://us.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]                                                  
Hit:3 http://archive.ubuntu.com/ubuntu bionic InRelease                                            
Get:4 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]        
Get:5 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]                                      
Fetched 252 kB in 1s (189 kB/s)    
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package sshpass
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package dirmngr
Hit:1 http://us.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]                                                  
Hit:3 http://archive.ubuntu.com/ubuntu bionic InRelease                                            
Get:4 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]        
Get:5 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]                                      
Fetched 252 kB in 1s (209 kB/s)    
Reading package lists... Done

Solution 1:

Probably what happens is that you have some Windows-style line endings in your file. Maybe the file was copied from a Windows system at some point, or you are using a text editor that uses Windows-style line endings by default? Anyway, I am able to reproduce the behavior if I add such line endings in a similar file.

One way to fix it is to use the dos2unix command to change all line endings in the file to unix-style line endings (just \n instead of \r\n at the end of each line). First install it:

sudo apt install dos2unix

Then run it on your file, if the filename is myfile.sh then just do this:

dos2unix myfile.sh

which should give an output like this:

dos2unix: converting file myfile.sh to Unix format...

and after that your script should work, you should no longer get the "E: Unable to locate package" errors.

The opposite command unix2dos is also available, you can use that if you want the problem to come back again. :-)

To play with it further, you can use for example hexdump -C myfile.sh to look at the difference before/after running dos2unix or unix2dos on the file.