Bash script not reading an alias in bashrc

I 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 the bash script (script.sh), this alias is not activated. How to ensure that aliases in a .bashrc file are accepted in a bash script ??

+7
bash alias
source share
5 answers

The alias does not expand in non-interactive shells.

The only way to make an alias is to specify the target script one that contains the alias.

 $ source .bashrc $ . custom_script.sh 
+5
source share

Quote from bash manual :

Aliases do not expand unless the shell is interactive, unless the expand_aliases shell parameter is set using shopt (see The Shopt Builtin).

Saying the following in your script should make it work:

 shopt -s expand_aliases 
+4
source share

Aliases are limited to the shell and do not work in shell executable scripts. You better create a variable.

+1
source share

You can run the script under bash bash interactively; add -i in bash e.g. script. Now you can use your aliases.

 #!/bin/bash -i alias lsd='ls -al | grep ^d' lsd 
0
source share

The simplest answer is to do 2 important things, or it will not work. In another script, do the following: -i for interactive mode and part of the store, as described below.

 #!/bin/bash -i # Expand aliases defined in the shell ~/.bashrc shopt -s expand_aliases 

After that, your aliases that you define in ~ / .bashrc will be available in your shell script (giga.sh or any.sh) and any function or child shell inside such a script.

If you do not, you will receive an error message:

 your_cool_alias: command not found 
0
source share

All Articles