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

妖精的绣舞 提交于 2021-02-10 13:19:07

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/34235425/how-to-source-an-external-file-in-bash-profile-in-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!