如何在OSX的.bash_profile中获取外部文件? [英] How to source an external file in .bash_profile in OSX?

查看:68
本文介绍了如何在OSX的.bash_profile中获取外部文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 .bash_profile 文件中定义了一些别名,并且别名可以正常工作.例如

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

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

而且,当我打开新终端时,键入 python-server 会打开一个以当前目录为根目录(或"/")的python服务器.

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

但是我大约有10个别名,我想备份别名,所以我想创建一个包含这些别名的外部文件,并试图像这样从.bash_profile中获取该文件

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

但是,我的命令运行正常.例如 python-server 可以从外部文件正常工作.我只想知道此错误的原因,可能是实现此错误的更好方法.

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.

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

别名文件的内容

#!/bin/bash

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

PS:现在,我从别名文件中删除了注释,并在以下情况下将:找不到命令的数量从 2 减少为 1 打开一个新的终端.

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.

推荐答案

从上面的文件中删除控制代码",即"\ r".

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

如何检查:

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

使用'vi'或'vim'编辑文件,或使用'tr'进行'清理'

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

tr -d '\r' ~/.bashprofile > ~/.bashprofile.mod  
cp ~/.bashprofile.mod ~/.bashprofile

这篇关于如何在OSX的.bash_profile中获取外部文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆