I have created an alias in the .bashrc file:
alias java='java -Xmx1200m'
This alias works when I run a java command from my shell directly.
However, when the java command is inside a bash script (script.sh), this alias does not get activated. How do I ensure that the aliases in .bashrc file are accepted in a bash script ??
A BASH Alias is a map of commands with the sets of commands or functions that can be used as a shortcut in the command line for a BASH environment. Bash Alias allows to aggregate multiple functions into a single command and also it avoids repetitive or large commands into a simple shortcut command.
bashrc . Thus, if you want to get the same behavior for both login shells and interactive non-login shells, you should put all of your commands in either . bashrc or . bash_profile , and then have the other file source the first one.
All you need to do is type alias at the prompt and any active aliases will be listed. Aliases are usually loaded at initialization of your shell so look in . bash_profile or . bashrc in your home directory.
Alias are not expanded in non-interactive shells.
The only way to make an alias is to source the target script with the one which contains the alias.
$ source .bashrc
$ . custom_script.sh
Quoting from the bash manual:
Aliases are not expanded when the shell is not interactive, unless the
expand_aliases
shell option is set usingshopt
(see The Shopt Builtin).
Saying the following in your script should make it work:
shopt -s expand_aliases
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With