Search code examples
bashnewlinealiasdos2unix

Bash alias file corrupted: No such file or directory


I have/had ~/.bashrc_aliases.sh (a file containing bash aliases) sourced by my ~/.bashrc file. This ~/.bashrc_aliases.sh file somehow happened to be in DOS format which I fixed by running dos2unix on the file.

When this alias file was in 'DOS' format, whenever I typed the alias command in Bash, the alias lines were starting with the ' character instead of a, and the aliases wouldn't really work, so I got stuff like:

": No such file or directory", "'s: invalid option -- '"

etc.

What was going on when the file was in DOS format? Why was the alias command returning lines starting with the ' character? Why was I getting the above error?


marked as duplicate by kenorb, tripleee bash

This question has been asked before and already has an answer. If those answers do not fully address your question, please edit this question to explain how it is different or ask a new question.

The question linked to was asking how to fix the problem. While this question was asking for an explanation as to what was happening during the problem, not how to fix it.


Solution

  • The first part is easier to answer. The DOS newline "character" is a pair \r\n (carriage return, linefeed). bash interprets the carriage return as just another character, so it is appended to the end of your alias. For example

    alias foo='echo foo'
    

    would append a trailing \r to the body of the alias. When displayed by the alias command, that carriage return would move the cursor to the beginning of the line before printing the final ' (which would overwrite the a in alias). That is, instead of

    $ alias
    alias foo='echo foo'
    

    you got

    $ alias
    'lias foo'=echo foo
    

    The errors probably depend on exactly what alias you were defining, but in each case the trailing carriage return affected the error message in the same way; the cursor moved to the beginning of the line and cause the end of the message to overwrite the beginning.