Is aliases available in shell scripts?
I define aliases and save in rc file. what I see is that it isn't available in shell scripts. They are however available in interpretive or interactive sub-shell which I get when I give command
Code:
bash Code:
I define |
Storing your aliases in an rc file (which one?) is not the way to go.
If you want to make them global I would suggest putting them in /etc/profile (or /etc/profile.d/). If you need them locally store them in ~/.profile (or ~/.bash_aliases). |
Also, expansion of aliases is usually disabled in non-interactive shells (scripts).
You can put Code:
shopt -s expand_aliases |
It's better to use functions than aliases.
And if you're wrapping a builtin, you could use builtin to call the builtin. Code:
function echo { |
konsolebox, please elaborate. What you mean by builtin
|
Oh yeah I didn't check. It's a feature in bash, and I'm referring to bash builtins. Run 'help builtin' if you're in bash. And run 'help' without any argument to see the list of those.
See the Bash manual for more info. |
Aliases are not enabled by default in shell scripts. As mentioned, they can be enabled with shopt, but even then that only makes it possible to create them in the script. You won't get immediate access to all the aliases in the main shell. Since aliases can't be exported, there's no way for a sub-process like a script to inherit a copy of them.
Aliases are rather pointless to use inside scripts anyway. Since you only need to write each command once, there's no real need for such user-interactive style shortcuts. It would also tend to make the code less obvious. As mentioned, if you need to define code that will be used multiple times, or want to simplify longer commands, use functions. BTW, if you just want to shorten up a longer command that has regularly-defined option strings, you can store them in an array first. Code:
options=( -x -n -a foobar --long-option=bazbum ) |
All times are GMT -5. The time now is 04:09 AM. |