Home > Mobile >  Creating Directory in Shell Causes Last Folder to have Extra "\r"
Creating Directory in Shell Causes Last Folder to have Extra "\r"

Time:09-25

I'm writing a basic shell script that creates directories and changes directories to that folder to write in it.

Right now I have the following:

mkdir -p "Home/first/second/third"
cd "Home/first/second/third"

This produces undesired results as the file structure looks as follows:

 - Home
   - first
   - second
   - third\r

I'm looking to get rid of the \r on the third\r but am unsure why it is doing so. I've tried a ton of different variations and can't seem to figure out what the issue is with adding that extra \r

CodePudding user response:

Are you sure that's the exact script?

Because the mkdir -p "Home/first/second/third" should and does create a structure like.

Home
  - first
    - second
      - third

You do need to clarify what OS and shell you are using.

Did you create the file on the host or copy it to the host from a windows machine?

-EDIT
Use

dos2unix script.sh

to get rid of those pesky \r characters that windows put in.

CodePudding user response:

Credit to @Klox for figuring this out.

Files edited on a Windows computer end in \r\n, while files edited on Linux computers end in \n.

Therefore, despite the code being compiled on Linux, it still ends created files with \r\n, causing the last directory created in a mkdir to end with \r. In order to remove this \r, files must be written in the linux environment.

  • Related