Search code examples
bashshellwindows-10shmobaxterm

Why is there no such file or directory_profile?


I am using Windows and MobaXterm.

I created a .bash_profile file in the ~ directory and the following line

alias sbp="source ~/.bash_profile"

is the only code in that file.

However, when I was trying to do sbp, I got an error.

enter image description here

This works on my Mac and it used to work on my old Windows computer (but that one has some water damage so it broke down). Why does this not work now?

Thanks in advance!


Solution

  • From the way that error message is garbled I'm pretty sure that the .bash_profile file you created has DOS/Windows-style line endings, consisting of a carriage return character followed by a newline character. Unix tools expect unix-style line endings consisting of just a newline; if they see DOS/Windows-style endings, they'll treat the carriage return as part of the content of the line. In this case, bash will treat the carriage return as part of the alias definition, and therefore part of the filename to filename to source. Try running alias sbp | cat -vt to print the alias with invisible characters shown; my guess is it'll print alias sbp='source ~/.bash_profile^M' (where the ^M is cat -vt's way of representing the carriage return).

    Solution: convert the file to unix format, and either switch to a text editor that knows how to save in unix format, or change your settings in the current editor to do it. For conversion, there are a number of semi-standard tools like dos2unix and fromdos. If you don't have any of those, this answer has some other options.

    BTW, the reason the error message is garbled is that the CR gets printed as part of the error message, and the terminal treats that as an instruction to go back to the beginning of the line; it then prints the rest of the message over top of the beginning of the message. It's a little like this:

    -bash: /home/dir/path/.bash_profile
    : No such file or directory
    

    ...but with the second line printed over the first, so it comes out as:

    : No such file or directory_profile