0

I'm trying to write simple shell script that clones a github repo then CDs into the folder. I have

#!/bin/bash
# $1 is github repo url
url=$1
reponame=$(basename $url)
git clone $url
echo $reponame
cd $reponame

Now when I run

bash deploy_app.sh https://github.com/username/reponame

It echoes the right $reponame but the next line returns this "cd: $'reponame\r\r\r\r': No such file or directory"

I have no idea where the "\r\r\r\r" part is coming from. It's not in the repo name and it's not part of $reponame when I just echo it. Any idea why this happens??

Magdi Gamal
  • 681
  • 1
  • 9
  • 25
  • Replace the line `echo $reponame` with `printf "%q\n" "$reponame"` and observe what is printed out. – M. Nejat Aydin Jul 26 '20 at 06:15
  • Your script file probably has DOS/Windows-style line endings; see ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – Gordon Davisson Jul 26 '20 at 06:47

2 Answers2

3

The first thing I would be looking at is your script itself, by doing something like:

od -xcb deploy_app.sh

and looking for any carriage return characters (CR, ^M, \r, hex 0d, or octal 015) at the end of the cd line (or any line really).

That's because, if you have CR characters at the end, it will cause that problem (the ^M characters below were entered with CTRL-VCTRL-M):

pax:~> cd blah^M^M^M^M
-bash: cd: $'blah\r\r\r\r': No such file or directory

If there are CR characters in your file, you probably want to remove them, and check your line ending settings for git (assuming that file is in a repo) - this is a problem commonly caused by incorrect settings if you're updating your repo from both Windows and Unix.

If that file is not being "damaged" by git, you'll need to discover what else is putting those characters in.


And, as an aside, you wouldn't actually see whether the reponame had those characters with an echo, since echoing them would result in the actual characters, four "go to the start of the line" characters, then a "go to the next line" character. Hence there's no way to tell the difference just by looking, without pushing the output through a command like od.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Whereas it is true that those characters cannot be *seen* with a plain `echo $reponame`, they can be *seen* with a `printf ` like that: `printf "%q\n" "$reponame"`. Or even with an `echo`: `echo ${reponame@Q}` – M. Nejat Aydin Jul 26 '20 at 05:39
  • Thanks!! I had the script inittally written in window then upload to the linux server. I rewrote it with the Nano in the server directly and it fixed the issue. – Magdi Gamal Jul 26 '20 at 07:46
0

I am not sure either, but try changing $reponame to "${reponame}" (with the double quotes) and see if it still happens.

petrus4
  • 616
  • 4
  • 7