在shell脚本中使用别名? [英] using alias in shell script?

查看:89
本文介绍了在shell脚本中使用别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在示例 shell 脚本中定义的别名不起作用.我是 Linux Shell 脚本的新手.下面是示例shell文件

My alias defined in a sample shell script is not working. And I am new to Linux Shell Scripting. Below is the sample shell file

#!/bin/sh

echo "Setting Sample aliases ..."
alias xyz="cd /home/usr/src/xyz"
echo "Setting done ..."

在执行此脚本时,我可以看到回显消息.但是如果我执行别名命令,我会看到下面的错误

On executing this script, I can see the echo messages. But if I execute the alias command, I see the below error

xyz: command not found

我错过了什么吗?

推荐答案

source 你的脚本,不要像 ./foo.sh 那样执行sh foo.sh

source your script, don't execute it like ./foo.sh or sh foo.sh

如果你这样执行你的脚本,它会在子 shell 中运行,而不是你当前的.

If you execute your script like that, it is running in sub-shell, not your current.

source foo.sh  

对你有用.

这篇关于在shell脚本中使用别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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