脚本失败的源bashrc [英] Source bashrc from script failing

查看:51
本文介绍了脚本失败的源bashrc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的bash脚本,该脚本会自动为.bashrc添加一个别名,完成后,我希望它获取.bashrc的源代码

I've written a simple bash script that adds an alias to my .bashrc automatically, and when it finishes, I would like it to source the .bashrc

例如到目前为止,它可以正常工作

It works fine as of now, for example

./addalias.sh ls 'ls -l' 

在.bashrc后面正确附加'alias ls ='ls -l',但不提供它.

properly appends 'alias ls='ls -l' to the .bashrc, but doesn't source it.

代码如下:

#!/bin/bash
FIRST=$1

SECOND=${2:-cd `pwd`}

echo alias $FIRST="'$SECOND'" >> /home/oscar/.bashrc
echo alias $FIRST="'$SECOND'"

source /home/oscar/.bashrc

那是行不通的,也不能运行别名("sourcebash")来提供bash而不是最后一行.

That doesn't work, nor does running an alias ("sourcebash") to source the bash instead of the last line.

关于如何解决此问题的任何想法?

Any thoughts on how this could be fixed?

推荐答案

运行'addalias.sh'的shell会获取 .bashrc 文件的源;然后退出.它不会也不会影响父shell的环境.

The shell that runs 'addalias.sh' does source the .bashrc file; it then exits. It does not and cannot affect the parent shell's environment.

您必须以以下方式调用命令:

You'd have to invoke the command as:

source ./addalias.sh ls 'ls --color=auto'

或者:

. ./addalias.sh ls 'ls --color=auto'

(现在已解决:我也不敢相信,即使在有问题的情况下,使用 sudo rm -fr/* 玩游戏也是非常明智的.存在太多风险的白痴复制并没有意识到.)

(Now fixed: And I'm not convinced that, even in a question, playing with sudo rm -fr /* is remotely sensible. There's too much risk of an idiot copying and not realizing.)

这篇关于脚本失败的源bashrc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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