Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an alias inside a bash shell script so that is it visible from the outside? [duplicate]

Tags:

bash

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?

like image 507
phil swenson Avatar asked Feb 04 '10 05:02

phil swenson


People also ask

Can you set an alias in a bash script?

How to set a Bash Alias? You can define a new alias by using the Bash command alias [alias_name]="[command]" . A Bash alias name cannot contain the characters / , $ , `` , = and any of the shell metacharacters or quoting characters. for the changes to take effect in your current terminal session.

How do I show my aliases in my shell?

To see all your current aliases: You can see that the alias l is a shortcut for the command ls -lg. The first word on each line is the name of the alias; the rest of the line is what gets executed when the alias is used.

Where do I put alias in bash?

Keeping bash alias in a different file Now you can add any aliases in your ~/. bash_aliases file and then load them into your Bash session with the source ~/. bashrc command.

How does alias work in bash?

A Bash alias is essentially nothing more than a keyboard shortcut, an abbreviation, a means of avoiding typing a long command sequence. If, for example, we include alias lm="ls -l | more" in the ~/. bashrc file, then each lm [1] typed at the command-line will automatically be replaced by a ls -l | more.


2 Answers

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.

like image 79
JoeB Avatar answered Sep 29 '22 20:09

JoeB


./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.

like image 40
codaddict Avatar answered Sep 29 '22 18:09

codaddict