Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 myexecutable

I 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 found

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

share|improve this question
    
How about adding the alias to .bash_aliases ? Also how about aliasing the absolute path instead of relative path like alias petsc='/home/user/petsc-3.2-p6/petsc-arch/bin/mpiexec' –  nitstorm Jan 26 '12 at 18:17
    
@nitstorm: Neither solution seems to work... I still get the same error as before –  Paul Jan 26 '12 at 18:21
add comment

4 Answers

up vote 11 down vote accepted
  1. In your shell script use the full path rather then an alias.

  2. In your shell script, set the alias, different syntax

    petsc='/home/your_user/petsc-3.2-p6/petsc-arch/bin/mpiexec`
    
    $petsc myexecutable
    
  3. Use a function in your script. Probably better if petsc is complex

    function petsc () {
        command 1
        command 2
    }
    
    petsc myexecutable
    
  4. Source 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.

share|improve this answer
    
Point 4 don't work, unless you use shopt -s expand_aliases in the same script. –  enzotib Jan 26 '12 at 18:29
    
Your suggestion 2) works, but I want to be able to use the same command in multiple shell scripts without having to write the first line petsc="...". Is there a way to do this? –  Paul Jan 26 '12 at 18:31
    
Sounds as if you should put the command in a more standard location, such as /usr/local/bin –  bodhi.zazen Jan 26 '12 at 18:35
    
@enzotib - thank you, I added that to my answer. –  bodhi.zazen Jan 26 '12 at 18:36
add comment

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.

share|improve this answer
    
That almost works... the only problem is that I need to be able to pass different flags to the executable "mpiexec"... For instance, I need to be able to execute something like "petsc -n 40 myexecutable" with the alias "petsc" –  Paul Jan 26 '12 at 18:46
    
@Paul: I added "$@" just to handle arguments. –  enzotib Jan 26 '12 at 19:47
add comment

Shell 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 mpiexec instead of petsc) Add $HOME/petsc-3.2-p6/petsc-arch/bin to your PATH variable. This can be done by editing ~/.profile and appending:

    PATH="$HOME/petsc-3.2-p6/petsc-arch/bin:$PATH"
    

    Re-login to apply these changes

  • Create the directory ~/bin and

    • make a wrapper script named petsc containing:

      #!/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
      
share|improve this answer
add comment

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

share|improve this answer
    
The reason why I can't put it into my $PATH is because I have another 'mpiexec' file in another directory that is alredy in my $PATH. If I add the path to this new 'mpiexec', it will probably try to execute both of them... wouldn't it? –  Paul Jan 26 '12 at 19:34
1  
Cannot understand why not simply use "$@". –  enzotib Jan 26 '12 at 19:49
1  
Paul, it'll try to execute the one that's first in the PATH, not both. –  unhammer Jan 27 '12 at 9:41
    
enzotib, ah I misread the way mpiexec was called, I thought it needed all args as one. Will edit my answer :) –  unhammer Jan 27 '12 at 9:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.