Jay Taylor's notes
back to listing indexHow to set an alias inside a bash shell script - Stack Overflow
[web search]
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! | |||
add comment | |||
sourcing the file using | |||||||||
|
You can write a function in your .profile to switch the aliases
then run you can run:
or
to switch back and forth. | |||||||||
|
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:
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.
| ||||
add comment |
If you need to switch Ruby versions, try rvm.
| |||||||||
|