How do I make aliases work across all the shells? - bash

I have been trying to make aliases work in bash shell. Now, let's say I do
alias yum='yum -v' in my shell. It works when I run the run the command from CLI. But when I run a script it doesn't have any effect. How can I make the aliases work across the shell?

In bash, you can export functions, so if you do:
yum() { command yum -v "$#"; }
export -f yum
Then the 'alias' for yum will persist in subshells. Note that functions are almost always preferred to aliases, and (from the bash man page) "For almost every purpose, aliases are superseded by shell functions.". I believe the initial clause 'For almost every purpose' is merely hyberbole and that 'almost' can be safely omitted.

From the bash manpage:
Aliases are not expanded when the shell is not interactive, unless the
expand_aliases shell option is set using shopt (see the description of
shopt under SHELL BUILTIN COMMANDS below).
As a result, you need to do the following inside your script.:
shopt -s expand_aliases
You will also need to source your file with aliases (.bashrc,.bash_profile,.bash_login etc).
shopt -s expand_aliases
source /path/to/alias/file
Please note that using alias inside your shell script will make it less portable as it will break when ran on environment that do not have your aliases.

Related

is is possible to use an alias in a shellscript [duplicate]

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.
You need to do shopt -s expand_aliases in the script in addition to sourcing ~/.bashrc.
The simplest answer is to do the 2 important things or it wont' work.
#!/bin/bash -i
# Expand aliases defined in the shell ~/.bashrc
shopt -s expand_aliases
After this, your aliases that you have defined in ~/.bashrc they will be available in your shell script (giga.sh or any.sh) and to any function or child shell within such script.
If you don't do that, you'll get an error:
your_cool_alias: command not found
.bashrc is meant for one purpose: to configure the environment for your interactive shells. If you have code that you want shared between your .bashrc and other scripts, then it belongs in a separate file that is sourced by each of your .bashrc and shell script.
I had this problem and I reloaded the file with this command to fix it.
$ source ~/.bashrc
Stolen from enzotib on ask ubuntu: Alias are deprecated in favor of shell functions. From bash manual page:
For almost every purpose, aliases are superseded by shell functions.
To create a function, and export it to subshells, put the following in your ~/.bashrc:
petsc() {
~/petsc-3.2-p6/petsc-arch/bin/mpiexec "$#"
}
export -f petsc
Then you can freely call your command from your scripts.
There is a way of doing it globally without adding lines to each script you execute: by using the BASH_ENV variable.
Here is my setup for OS X 10.8.5:
/etc/launchd.conf:
setenv BASH_ENV /Users/DK/.env
~/.bash_profile:
# == Bash setup for interactive shells ==
# === Environment for all shells ===
. $BASH_ENV
# [skipped]
~/.env:
# == Setup for all shells ==
# This is executed for all interactive and for non-interactive shells (e.g. scripts)
shopt -s expand_aliases extglob xpg_echo
# [skipped] Misc. variables and PATH setup
# === General aliases ===
alias pause='echo "Press [Return] or [Enter] to continue..."; read' # read -p does not display prompt in Eclipse console
# [skipped]

Running bash builtin command "type" from script does not work for aliases [duplicate]

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.
You need to do shopt -s expand_aliases in the script in addition to sourcing ~/.bashrc.
The simplest answer is to do the 2 important things or it wont' work.
#!/bin/bash -i
# Expand aliases defined in the shell ~/.bashrc
shopt -s expand_aliases
After this, your aliases that you have defined in ~/.bashrc they will be available in your shell script (giga.sh or any.sh) and to any function or child shell within such script.
If you don't do that, you'll get an error:
your_cool_alias: command not found
.bashrc is meant for one purpose: to configure the environment for your interactive shells. If you have code that you want shared between your .bashrc and other scripts, then it belongs in a separate file that is sourced by each of your .bashrc and shell script.
I had this problem and I reloaded the file with this command to fix it.
$ source ~/.bashrc
Stolen from enzotib on ask ubuntu: Alias are deprecated in favor of shell functions. From bash manual page:
For almost every purpose, aliases are superseded by shell functions.
To create a function, and export it to subshells, put the following in your ~/.bashrc:
petsc() {
~/petsc-3.2-p6/petsc-arch/bin/mpiexec "$#"
}
export -f petsc
Then you can freely call your command from your scripts.
There is a way of doing it globally without adding lines to each script you execute: by using the BASH_ENV variable.
Here is my setup for OS X 10.8.5:
/etc/launchd.conf:
setenv BASH_ENV /Users/DK/.env
~/.bash_profile:
# == Bash setup for interactive shells ==
# === Environment for all shells ===
. $BASH_ENV
# [skipped]
~/.env:
# == Setup for all shells ==
# This is executed for all interactive and for non-interactive shells (e.g. scripts)
shopt -s expand_aliases extglob xpg_echo
# [skipped] Misc. variables and PATH setup
# === General aliases ===
alias pause='echo "Press [Return] or [Enter] to continue..."; read' # read -p does not display prompt in Eclipse console
# [skipped]

shell issue: alias doesn't expand

I have the following problem:
I have many test scripts that are currently running using MKS Toolkit. We managed to run those scripts using the mingw shell (msys) but there are other scripts that make use of some aliases that are built-in MKS Toolkit and not in bash.
The problem is that nobody wants to change those scripts, not even automatically by using a script. This means that I should define and use those aliases. I tried defining the aliases in the "/etc/profile" file of msys shell but without any success. I also tried to use shopt -s expand_aliases (in "/etc/profile") but that doesn't work either.
Could someone share an idea on how this could be done. Mind that the existing scripts will have to remain the same.
Any thoughts or ideas are welcome.
Thanks
The /etc/profile is only sourced for login shells. If you want the aliases in your script, you should put them in a separate file and source them into the script(s) that need them. For example:
aliases.sh:
alias walrus="echo coo coo cah joo"
script.sh:
#!/bin/bash
shopt -s expand_aliases
. aliases.sh
walrus
and then
$ ./script.sh
coo coo cah joo
That's probably the most reasonable way to do it. If you insist on not changing the scripts at all, then you might be able to get away with executing them indirectly like:
bash --rcfile aliases.sh -i script.sh
That will tell bash to execute an interactive shell (and thus expand aliases and source our aliases script before executing script.sh).
This answer gave me the solution. It involves defining the variable BASH_ENV before executing
bash script_name.sh
BASH_ENV will point to a script which sets the environment of the shell. In my case, to export the aliases and also the functions needed the script looks like this:
shopt -s expand_aliases
alias my_alias="echo my_alias"
function MyFunction {
echo "MyFunction(DUMB):" $*
}
export -f MyFunction

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

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.
You need to do shopt -s expand_aliases in the script in addition to sourcing ~/.bashrc.
The simplest answer is to do the 2 important things or it wont' work.
#!/bin/bash -i
# Expand aliases defined in the shell ~/.bashrc
shopt -s expand_aliases
After this, your aliases that you have defined in ~/.bashrc they will be available in your shell script (giga.sh or any.sh) and to any function or child shell within such script.
If you don't do that, you'll get an error:
your_cool_alias: command not found
.bashrc is meant for one purpose: to configure the environment for your interactive shells. If you have code that you want shared between your .bashrc and other scripts, then it belongs in a separate file that is sourced by each of your .bashrc and shell script.
I had this problem and I reloaded the file with this command to fix it.
$ source ~/.bashrc
Stolen from enzotib on ask ubuntu: Alias are deprecated in favor of shell functions. From bash manual page:
For almost every purpose, aliases are superseded by shell functions.
To create a function, and export it to subshells, put the following in your ~/.bashrc:
petsc() {
~/petsc-3.2-p6/petsc-arch/bin/mpiexec "$#"
}
export -f petsc
Then you can freely call your command from your scripts.
There is a way of doing it globally without adding lines to each script you execute: by using the BASH_ENV variable.
Here is my setup for OS X 10.8.5:
/etc/launchd.conf:
setenv BASH_ENV /Users/DK/.env
~/.bash_profile:
# == Bash setup for interactive shells ==
# === Environment for all shells ===
. $BASH_ENV
# [skipped]
~/.env:
# == Setup for all shells ==
# This is executed for all interactive and for non-interactive shells (e.g. scripts)
shopt -s expand_aliases extglob xpg_echo
# [skipped] Misc. variables and PATH setup
# === General aliases ===
alias pause='echo "Press [Return] or [Enter] to continue..."; read' # read -p does not display prompt in Eclipse console
# [skipped]

Why aliases in a non-interactive Bash shell do not work

I am trying to use aliases in a non-interactive bash shell. I have defined my aliases in ~/.bashrc and I have set the variable BASH_ENV=~/startUpFile. The contents of the startUpFile are source ~/.bashrc.
I can see that my aliases are recognized, when I execute the alias command. However, if I try to use an alias defined in ~/.bashrc, Bash can't recognized it. It gives me the unknown command error.
With the TCSH shell it is pretty easy to do this because the ~/.cshrc file is always read.
Any ideas how I can do this with a Bash shell?
The command shopt -s expand_aliases will allow alias expansion in non-interactive shells.
.bashrc is only processed by interactive shells.
In addition, aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt. Unless, of course, POSIX mode is invoked by calling the shell with the name sh instead of bash.
People who use aliases a lot often source their .bashrc at the end of their profile so that the aliases are there even for non-interactive shells. This might not be the best way, but it is pretty common.
It's things like this that lead me to believe that in the 21st century we should abandon shell scripts in favor of a full-blown language like Python. It's a lot more predictable.
You have to
shopt -s expand_aliases
in the file pointed to in your BASH_ENV
I had similar issue, in the end, I found out that ~/.bashrc was all I needed.
However, in Ubuntu, I had to comment the line that stops processing ~/.bashrc :
If not running interactively, don't do anything
[ -z "$PS1" ] && return

Resources