Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script not reading alias in bashrc [duplicate]

Tags:

alias

bash

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 ??

like image 451
prathmesh.kallurkar Avatar asked Jul 18 '13 13:07

prathmesh.kallurkar


People also ask

Does alias work in 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.

Should aliases go in Bash_profile or bashrc?

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.

How can I see my aliases in bash?

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.


2 Answers

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
like image 137
epsilon Avatar answered Oct 19 '22 10:10

epsilon


Quoting from the bash manual:

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt (see The Shopt Builtin).

Saying the following in your script should make it work:

shopt -s expand_aliases
like image 26
devnull Avatar answered Oct 19 '22 11:10

devnull