简体   繁体   English

在bash函数中使用别名

[英]Using alias in bash function

I have defined an alias like so: 我已经定义了一个别名,如下所示:

alias X="path/to/program"

and I have a function defined like this: 我有一个这样定义的函数:

doX() { X -flag "$1"; }

I put these in my .bashrc file, and when I open bash, I get a syntax error near unexpected token '-flag'. 我将它们放在我的.bashrc文件中,当我打开bash时,在意外的标记'-flag'附近收到语法错误。 At this point, the alias has been set, but the function has not, due to this error. 此时,由于此错误,已经设置了别名,但尚未设置函数。 If I run 如果我跑步

doX() { X -flag "$1"; }

at this point, it works. 在这一点上,它起作用。 I have tried putting this into a file and sourcing it after I set the alias in the .bashrc file, but it is giving me the same results. 在.bashrc文件中设置别名后,我曾尝试将其放入文件中并进行采购,但是它给了我相同的结果。

How can I fix this? 我怎样才能解决这个问题? Is there a way to define the alias AND the function in the .bashrc so that they are both set when I open bash? 有没有一种方法可以定义别名和.bashrc中的函数,以便在我打开bash时都将它们都设置好?

Aliases are not usually available in scripts. 别名通常在脚本中不可用。 If you want to have a function use an alias, consider making the alias itself a function: 如果要让函数使用别名,请考虑使别名本身成为函数:

X() { path/to/program "$@"; }
doX() { X -flag "$1"; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM