Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use aliases defined in .bashrc in other scripts?

Tags:

alias

bash

In ~/.bashrc, I defined some aliases. But I cannot use them in other shell scripts, where I can only use aliases defined right there. Even though I sourced bashrc, it still did not work. What should I do?

PS. I'm in bash.

like image 602
Dutor Avatar asked Mar 09 '11 02:03

Dutor


People also ask

Can you use aliases in bash scripts?

Bash Alias allows to aggregate multiple functions into a single command and also it avoids repetitive or large commands into a simple shortcut command. BASH Alias is a map of shortcut command with the sets of commands which are stored in a file called either . bash_profile or . bashrc with a specific syntax.

Should aliases go in bashrc or Bash_profile?

bashrc or in ~/. bash_aliases but it should not go in . profile . If the variables are only ever needed in shells where the aliases (which expand to commands that use them) are available, then it is actually fine to put them in the same place as the aliases.

Where do I put bash aliases?

bash_aliases is present under the user home directory and load it whenever you initiate a new terminal session. You can also create a custom alias file under any directory and add definition in either . bashrc or . profile to load it.


1 Answers

You need to do shopt -s expand_aliases in the script in addition to sourcing ~/.bashrc.

like image 96
Dennis Williamson Avatar answered Sep 22 '22 20:09

Dennis Williamson