Linux – Windows 10 Ubuntu Bash: Aliases not working (: command not found)

bashbash-aliaswindows-10-v1607windows-subsystem-for-linux

I started the Ubuntu bash on Windows that was shipped with the Anniversary Update 1607. This bash sources the .bashrc file located at /home/phil which is the home directory of the Unix user, not the Windows user (this would be /mnt/c/Users/Philipp). In this file is among other things the following piece of code:

if [ -f .bash_aliases ]; then
    . .bash_aliases
fi

Since I already have a aliases file in my Windows home directory, I replaced that piece with:

if [ -f /mnt/c/Users/Philipp/.aliases_win ]; then
     . /mnt/c/Users/Philipp/.aliases_win
fi

Which when restarting bash or sourcing directly with source /mnt/c/Users/Philipp/.aliases_win gives:

: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found

So I removed as many aliases until these error messages disappeared, leaving only these aliases in the file:

(For reference, the complete file looks like this: https://github.com/kleinfreund/dotfiles/blob/master/win/bash/.aliases)

# Quickly navigate up the *great hollow*
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'

But upon using:

phil@RAUMSTATION:~$ ..
: File or directory not found

However when I do alias ..='cd ..' directly from within bash, it works fine.

I'm now utterly confused. What am I doing wrong? The aliases file works fine on my Ubuntu installation and also with things like Git Bash.

Edit: Putting my aliases into /home/phil/.bashrc works, not so much when sourcing the file from my Windows users’ home directory.

Best Answer

You need to convert the line endings of /mnt/c/Users/Philipp/.aliases_win from windows format (CRLF) to unix format (LF). You can do this with sed (this will overwrite the file):

sed -i 's/\r$//' /mnt/c/Users/Philipp/.aliases_win

Explanation:

The reason bash says : command not found is because it interprets the carriage return on the "blank" lines in your windows-formatted file as the name of a command. You can test this for yourself in the bash shell by typing CTRL-V, CTRL-M and then hitting enter (CTRL-M or ^M corresponds to a carriage return, see man console_codes).

Once you removed all the blank lines, you were left with just a few alias commands, which gave you : File or directory not found after executing the aliases because the carriage return on the end of the alias lines was actually added into the alias. For example, the line alias ..='cd ..'<CR><LF> made .. an alias for cd ..<CR>, which does not go to the parent folder .. but instead tries to change to the directory with the name ..<CR>. The error message when running this alias doesn't start with the typical bash: cd: foldername because echoing the carriage return in the folder name causes the cursor to return to the beginning of the line; try something like echo aaaa<CR>bb to see this more clearly.

Related Question