bash 如何在其他脚本中使用 .bashrc 中定义的别名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5240755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:35:41  来源:igfitidea点击:

How to use aliases defined in .bashrc in other scripts?

bashalias

提问by Dutor

In ~/.bashrc, I defined some aliases. But I cannot use them in other shell scripts, where I can only use aliases defined right there. Even though I sourced bashrc, it still did not work. What should I do?

在 ~/.bashrc 中,我定义了一些别名。但是我不能在其他 shell 脚本中使用它们,在那里我只能使用在那里定义的别名。即使我使用了 bashrc,它仍然无法正常工作。我该怎么办?

PS. I'm in bash.

附注。我在狂欢。

采纳答案by Paused until further notice.

You need to do shopt -s expand_aliasesin the script in addition to sourcing ~/.bashrc.

shopt -s expand_aliases除了采购外,您还需要在脚本中做~/.bashrc

回答by squirtleBoy

I had this problem and I reloaded the file with this command to fix it.

我遇到了这个问题,我用这个命令重新加载了文件来修复它。

$ source ~/.bashrc 

回答by Trenton

Stolen from enzotibon ask ubuntu: Alias are deprecated in favor of shell functions. From bashmanual page:

在询问 ubuntu 时从enzotib 窃取:别名已被弃用,以支持 shell 函数。从bash手册页:

For almost every purpose, aliases are superseded by shell functions.

对于几乎所有目的,别名都被 shell 函数取代。

To create a function, and export it to subshells, put the following in your ~/.bashrc:

要创建一个函数并将其导出到子shell,请将以下内容放入您的~/.bashrc

petsc() {
    ~/petsc-3.2-p6/petsc-arch/bin/mpiexec "$@"
}
export -f petsc

Then you can freely call your command from your scripts.

然后您可以从脚本中自由调用您的命令。

回答by AKS

The simplest answer is to do the 2 important things or it wont' work.

最简单的答案是做两件重要的事情,否则就行不通。

#!/bin/bash -i

# Expand aliases defined in the shell ~/.bashrc
shopt -s expand_aliases

After this, your aliases that you have defined in ~/.bashrc they will be available in your shell script (giga.sh or any.sh) and to any function or child shell within such script.

在此之后,您在 ~/.bashrc 中定义的别名将在您的 shell 脚本(giga.sh 或 any.sh)以及此类脚本中的任何函数或子 shell 中可用。

If you don't do that, you'll get an error:

如果你不这样做,你会得到一个错误:

your_cool_alias: command not found

回答by chepner

.bashrcis meant for one purpose: to configure the environment for your interactive shells. If you have code that you want shared between your .bashrcand other scripts, then it belongs in a separate file that is sourced by eachof your .bashrcand shell script.

.bashrc仅用于一个目的:为交互式 shell 配置环境。如果您希望在您的.bashrc脚本和其他脚本之间共享代码,那么它属于一个单独的文件,该文件由您的每个.bashrc脚本和 shell 脚本提供。

回答by DKroot

There is a way of doing it globally without adding lines to each script you execute: by using the BASH_ENVvariable.

有一种方法可以全局执行此操作,而无需向您执行的每个脚本添加行:通过使用BASH_ENV变量。

Here is my setup for OS X 10.8.5:

这是我对 OS X 10.8.5 的设置:

/etc/launchd.conf:

/etc/launchd.conf:

setenv BASH_ENV /Users/DK/.env

~/.bash_profile:

~/.bash_profile:

# == Bash setup for interactive shells ==    
# === Environment for all shells ===
. $BASH_ENV    
# [skipped]

~/.env:

~/.env:

# == Setup for all shells ==
# This is executed for all interactive and for non-interactive shells (e.g. scripts)

shopt -s expand_aliases extglob xpg_echo

# [skipped] Misc. variables and PATH setup

# === General aliases ===

alias pause='echo "Press [Return] or [Enter] to continue..."; read' # read -p does not display prompt in Eclipse console

# [skipped]