Abraham
Abraham

Reputation: 217

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?

Upvotes: 2

Views: 1541

Answers (1)

Fengyang Wang
Fengyang Wang

Reputation: 12051

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

Upvotes: 0

Related Questions