I have an executable file mpiexec, whose full path is ~/petsc-3.2-p6/petsc-arch/bin/mpiexec. Since I want to execute this command in different directories (without having to retype the entire path), I setup an alias in my home .bashrc file:
alias petsc="~/petsc-3.2-p6/petsc-arch/bin/mpiexec" which allows me to execute this mpiexec file at the command prompt easily by typing:
petsc myexecutableI tried to write a shell script file, named script, using my new alias petsc as a command. After giving my shell script the appropriate permissions (using chmod), I tried to run the script. However, it gave me the following error:
./script: line 1: petsc: command not foundI know that I could just write the full path to the mpiexec file, but it is cumbersome to write the full path everytime that I want to write a new script. Is there a way that I can use my alias petsc inside the script file? Is there a way I can edit my .bashrc or .bash_profile to make this happen?
11 Answers
Some options:
In your shell script use the full path rather then an alias.
In your shell script, set a variable, different syntax
petsc='/home/your_user/petsc-3.2-p6/petsc-arch/bin/mpiexec' $petsc myexecutableUse a function in your script. Probably better if
petscis complexfunction petsc () { command 1 command 2 } petsc myexecutableSource your aliases
shopt -s expand_aliases source /home/your_user/.bashrc
You probably do not want to source your .bashrc, so IMO one of the first 3 would be better.
Aliases are deprecated in favor of shell functions. From the 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 petscThen you can freely call your command from your shell scripts.
4In bash 4 you can use special variable: $BASH_ALIASES.
For example:
$ alias foo="echo test"
$ echo ${BASH_ALIASES[foo]}
echo test
$ echo `${BASH_ALIASES[foo]}` bar
test barAlternatively define as variable then use command substitution or eval.
So for example, instead of defining the alias such as:
alias foo="echo test"define it as:
foo="echo test"instead. Then execute it by either:
find . -type f -exec sh -c "eval $foo" \;or:
find . -type f -exec sh -c "echo `$foo`" \; 3 ALIASES ... 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).So the real answer to this question, for those looking to use actual aliases in shell scripts instead of alternatives to them, is:
#!/bin/bash
shopt -s expand_aliases
alias foo=bar
foo whateverAs for why I'd want to do this: Due to unusual circumstances, I need to trick a Dockerfile into thinking it's a shell script.
3Shell functions and aliases are limited to the shell and do not work in executed shell scripts. Alternatives for your case:
(if you do not bother to use
mpiexecinstead ofpetsc) Add$HOME/petsc-3.2-p6/petsc-arch/binto yourPATHvariable. This can be done by editing~/.profileand appending:PATH="$HOME/petsc-3.2-p6/petsc-arch/bin:$PATH"Re-login to apply these changes
Create the directory
~/binandmake a wrapper script named
petsccontaining:#!/bin/sh exec ~/petsc-3.2-p6/petsc-arch/bin/mpiexec "$@"if the program allows for it, you can skip the shellscript and make a symlink using the command:
ln -s ~/petsc-3.2-p6/petsc-arch/bin/mpiexec ~/bin/petsc
You can force bash to execute your script as an interactive shell with the -i flag. This will tell your .bashrc file to define aliases and other functions.
Example:
~ $ grep ll .bashrc
alias ll='ls -lah'
~ $ cat script.sh
#!/bin/sh
ll
~ $ bash script.sh
script.sh: line 3: ll: command not found
~ $ bash -i script.sh
..directory contents..More info:
$ man bash 1 In
.bash_aliases:petsc { ~/petsc-3.2-p6/petsc-arch/bin/mpiexec "$@" }Or put the function in
.bashrc. Usually in.bashrconly config settings ofbashare stored.In Terminal:
source .bash_aliases- Call it:
petsc arg(s)
Advantage: you do not need export -f petscin .bash_aliases. Aliases are deprecated but using .bash_aliases for functions is ok.
- Use your aliases in your shell script.
- Source your script in your current, interactive shell instead of executing it.
So if you have a file called script.sh with your commands that include using aliases, simply type:
source script.sh 2 (EDIT: removed functions since I misread calling of mpiexec.)
If the only thing you need is less typing, why don't you just put the folder in $PATH? Or make a symlink to mpiexec from some folder in $PATH? Or (my favourite) put the alias in a script that you source in the calling script?
4Please use #!/usr/bin/env sh instead of #!/usr/bin/env bash, then it works.
sample: my.sh
#!/usr/bin/env sh
alias k="kubectl --kubeconfig ~/shop/config"
k version./my.shIf you have to use #!/usr/bin/env bash, then execute the script as the following way:
sh my.sh You can use case statement like this when in a sub-shell:
function execute() { case $1 in ls) $@ --color=auto ;; grep) $@ --color=auto ;; fgrep) $@ --color=auto ;; egrep) $@ --color=auto ;; *) $@ ;; esac
}
( execute $@ )