1

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.

Community
  • 1
  • 1
Bryan Ritter
  • 123
  • 1
  • 7

2 Answers2

3

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.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Isn't the ' character before the '\r'? Why would Bash do the '\r' first? – Bryan Ritter Sep 02 '14 at 17:56
  • 1
    Because of quote removal, `'echo foo'\r` produces the string `echo foo\r`. You would get the same result with `alias foo="echo foo"` or even `alias foo=echo\ foo`. The use of single quotes in the output of `alias` is unrelated to which type of quotes you used when defining the alias. – chepner Sep 02 '14 at 17:58
1

Terminals interpret \r as "go to the beginnining of the current line, (and start overwriting characters there)"

Most terminals interpret \n as "go down one line in the same column", but have a (usually-on) configuration bit in termios that transforms \n to \r\n.

(There are also the C1 controls IND = '\x84' and NEL = '\x85' to be unambiguous, but nobody uses them)

o11c
  • 15,265
  • 4
  • 50
  • 75