Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have an alias for a command (I'm setting up a Python development environment)

alias python=~/virtualenv/bin/python

so that I can run ~/virtualenv/bin/python by just typing python. Now in my project there is a shell script that goes, for example:

#!/bin/sh
python run-project.py

Can I make the script use my aliased python instead of the python it finds in $PATH, without making changes to the script?

share|improve this question
4  
Out of interest, why can't you change PATH? Changing PATH and putting #!/usr/bin/env python at the top of your python scripts seems easier. –  Mikel Mar 2 '11 at 2:55
    
@Mikel This is even better than your other answer. Can't accept comments, though. –  phunehehe Mar 2 '11 at 4:28
    
I updated my answer to include that suggestion. –  Mikel Mar 2 '11 at 4:38

2 Answers 2

up vote 5 down vote accepted

Yes.

If you put your aliases in ~/.aliases, then you can do

export BASH_ENV="~/.aliases"
somescript

This assumes your script starts with #!/bin/bash, because #!/bin/sh is a little less predictable.

Here's what I'd suggest:

  1. Create ~/.bashenv
  2. Move all the settings that you want to work in scripts from ~/.bashrc into ~/.bashenv
  3. Add this at the top of your ~/.bashrc:
    [ -f ~/.bashenv ] && source ~/.bashenv
  4. Put BASH_ENV=~/.bashenv in /etc/environment
  5. Make your scripts start with #!/bin/bash if they don't already

Or, if you're using zsh, just move your aliases into ~/.zshenv. zsh looks in that file automatically.


But maybe it's easier to just put ~/virtualenv/bin near the front of your PATH, then change your Python scripts to have #!/usr/bin/env python as the first line.

share|improve this answer
    
Don't you mean ~/.bashenv in step 4? –  cjm Mar 2 '11 at 2:47
    
So I'll have to change the script anyway, from #!/bin/sh to #!/bin/bash? Can I just do bash my-script? –  phunehehe Mar 2 '11 at 2:48
    
I do! Thanks. Fixed. –  Mikel Mar 2 '11 at 2:49
    
phunehehe: Yes, I think bash my-script will work if you still do steps 1-4. –  Mikel Mar 2 '11 at 2:50
    
I don't recommend using aliases in scripts. It won't work if someone else runs your script, or if you run the script under sudo or other privilege elevation method. And it doesn't allow you to change the virtualenv per execution, which would be the main point of not having ~/virtualenv/bin in your PATH permanently. Adding the directory in front of the PATH when needed is the best method. –  Gilles Mar 2 '11 at 8:02

Supposing that your alias file is "~/.bash_aliases", put this in your script:

#!/bin/bash
shopt -s expand_aliases
source ~/.bash_aliases
python run-project.py

(via)

share|improve this answer

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.