加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

别名在Bash脚本中不起作用

发布时间:2020-12-16 01:50:36 所属栏目:安全 来源:网络整理
导读:我有一个可执行文件command.sh #/bin/bashalias my_command='echo ok'my_command 我的终端是bash. 当我像./command.sh一样运行时,它运行正常. 当我像/ bin / bash ./command.sh一样运行它时,它找不到my_command可执行文件. 当我像/ bin / sh ./command.sh一
我有一个可执行文件command.sh

#/bin/bash
alias my_command='echo ok'
my_command

我的终端是bash.

当我像./command.sh一样运行时,它运行正常.

当我像/ bin / bash ./command.sh一样运行它时,它找不到my_command可执行文件.

当我像/ bin / sh ./command.sh一样运行它时,它运行正常.

我在这里很困惑.哪里有问题?

解决方法

从bash手册页:

Aliases are not expanded when the shell is not interactive,unless the expand_aliases shell option is set using shopt (see the
description of shopt under SHELL BUILTIN COMMANDS below).

换句话说,默认情况下,bash shell脚本中未启用别名.使用bash运行脚本时,它会失败.

你的sh似乎默认允许在脚本中使用别名.使用sh运行脚本时,它会成功.

./command.sh碰巧工作,因为你的shebang格式不正确(你在#!/ bin / bash中错过了!).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读