Jay Taylor's notes

back to listing index

How to set an alias inside a bash shell script - Stack Overflow

[web search]
Original source (stackoverflow.com)
Tags: programming bash shell shopt aliases context stackoverflow.com
Clipped on: 2012-11-01

OSX: This works from the command line: alias ruby="/opt/local/bin/ruby1.9"

but in side a shell script, it has no effect. I want to write a script that will switch between ruby 1.8 and ruby 1.9, so this needs to be a script - not in my profile.

It appears "source script.sh" works, but "./script.sh". Why is this? How can I replicate this in my script?

thanks!

asked Feb 4 '10 at 5:16
Image (Asset 1/7) alt= 1,27232137

69% accept rate
add comment

4 Answers

up vote 18 down vote accepted

./script.sh will be executed in a sub-shell and the changes made apply only the to sub-shell. Once the command terminates, the sub-shell goes and so do the changes.

sourcing the file using . ./script.sh or source ./script.sh will read and execute commands from the file-name argument in the current shell context, that is when a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes.

answered Feb 4 '10 at 5:23
Image (Asset 2/7) alt= 94.5k20156264
  upvote
 flag
Nice, I didn't know this command source, thank you :) – javano Sep 17 at 8:35
add comment

You can write a function in your .profile to switch the aliases

function toggle-ruby() {
  if [ "$1" == "1.9" ]; then
    alias ruby=/opt/local/bin/ruby1.9
  else
    alias ruby=/opt/local/bin/ruby1.8
  fi
}

then run you can run:

toggle-ruby 1.9

or

toggle-ruby 1.8

to switch back and forth.

answered Feb 4 '10 at 5:44
Image (Asset 3/7) alt= 4,82811120
  upvote
 flag
this is a good approach. thanks! – phil swenson Feb 4 '10 at 14:25
add comment

The simple answer for you is that scripts create non-interactive shells and, by default, the expand_aliases option is often disabled.

You can fix this very simply by just adding the following line to the top of your script to enable the alias expansion:

shopt -s expand_aliases

This problem has been bugging me, so I did research and then wrote a blog post once I figured out how to fix it for myself: Post about using alias from within Linux shell scripts.

Of course, right after I figured out that part, I found that, while it works for what you need, it will not work if you have a subshell within a a subshell. I am still looking into the fix for that problem, that is how I just came across your question. On the blog post, I mention a cheap hack that I use to grab the alias in a shell script. It isn't elegant, but it actually works even in this multiple subshell problem I have.

Image (Asset 4/7) alt= 2,028930
answered Jul 28 '10 at 16:11
Image (Asset 5/7) alt= 9112
add comment

If you need to switch Ruby versions, try rvm.

Image (Asset 6/7) alt= 8,93743059
answered May 26 at 2:32
Image (Asset 7/7) alt= 211
  upvote
 flag
One million upvotes. – Ziggy Jul 7 at 23:34
add comment

Your Answer

 
community wiki

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