简体   繁体   中英

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'. 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.

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?

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"; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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