Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to source an external file in .bash_profile in OSX?

I have defined some aliases in my .bash_profile file and aliases work as expected. e.g

alias python-server="python -m SimpleHTTPServer 7070"

And, When I open new terminal, typing python-server opens up a python server with current directory as root (or "/").

But I have around 10 aliases and I want to backup the aliases So I thought to create an external file which contain these aliases and am trying to source that file from .bash_profile like this

source ~/personal/Dropbox/scripts/aliases.sh

But when I open the new terminal I receive the errors

Last login: Fri Dec 11 23:16:28 on ttys004
: No such file or directory
: command not found
: command not found

However, my commands are working fine. e.g. python-server works as expected from external file. I just want to know the reason of this error and may be a better way to achieve this.

Contents of .bash_profile

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

# Load aliases
source ~/personal/repo/scripts/aliases

PATH=$PATH:$HOME/personal/repo/scripts/commands
PATH=$PATH:$HOME/personal/repo/scripts/git
export PATH

Contents of alias file

#!/bin/bash

# ---------------
# Load my aliases
# ---------------
alias python-server="python -m SimpleHTTPServer 7070"

PS: Now, I removed the comment from aliases file and it reduced the count of : command not found from 2 to 1 when opening a new terminal.

like image 583
Sachin Avatar asked Sep 12 '25 20:09

Sachin


1 Answers

Remove 'control codes', i.e. '\r' from your files above.

How to check:

  cat ~/.bashprofile  | od -c ## see any '\r's?

Use 'vi' or 'vim' to edit the files or 'clean' using 'tr', i.e.

tr -d '\r' ~/.bashprofile > ~/.bashprofile.mod  
cp ~/.bashprofile.mod ~/.bashprofile
like image 74
Dale_Reagan Avatar answered Sep 15 '25 11:09

Dale_Reagan