using alias in shell script? [duplicate]
Asked Answered
Z

7

30

My alias defined in a sample shell script is not working. And I am new to Linux Shell Scripting. Below is the sample shell file

#!/bin/sh

echo "Setting Sample aliases ..."
alias xyz="cd /home/usr/src/xyz"
echo "Setting done ..."

On executing this script, I can see the echo messages. But if I execute the alias command, I see the below error

xyz: command not found

am I missing something ?

Zeena answered 12/4, 2013 at 9:45 Comment(3)
An alias is a way of shortening a command. (They are only used in interactive shells and not in scripts — this is one of the very few differences between a script and an interactive shell.)Capuche
Why doesn't my Bash script recognize aliases?Capuche
The alias was defined in the shell that executed the script, not the shell from which you executed the script.Ulu
B
52

source your script, don't execute it like ./foo.sh or sh foo.sh

If you execute your script like that, it is running in sub-shell, not your current.

source foo.sh  

would work for you.

Breathy answered 12/4, 2013 at 9:51 Comment(1)
Problem of this approach is that all the script variable will invade your environment. which is not what you want!Leonelleonelle
L
34

You need to set a specific option to do so, expand_aliases:

 shopt -s expand_aliases

Example:

# With option
$ cat a
#!/bin/bash
shopt -s expand_aliases
alias a="echo b"
type a
a
$ ./a
# a is aliased to 'echo b'
b

# Without option
$ cat a
#!/bin/bash
alias a="echo b"
type a
a

$ ./a
./a: line 3: type: a: not found
./a: line 4: a: command not found

reference: https://unix.stackexchange.com/a/1498/27031 and https://askubuntu.com/a/98786/127746

Leonelleonelle answered 19/6, 2015 at 18:7 Comment(1)
OP isn't trying to use the alias in the script, but after the script has completed.Ulu
B
3

sourcing the script source script.sh

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


OR

HACK: Simply run following command on shell and then execute the script.

alias xyz="cd /home/usr/src/xyz"
./script.sh

To unalias use following on shell prompt

unalias xyz 
Blessington answered 12/4, 2013 at 9:51 Comment(3)
And how does executing alias xyz="cd /home/usr/src/xyz" add to ~/.bashrc?Countess
Sorry, but for me the $alias, $unalias and $source make no sense. Why are they variables? Unless you explain that, I downvote.Whidah
$ is just being used to represent the prompt in the command line (like in some of the answers above). Additionally, I don't think SO encourages users to downvote things just because they don't understand them.Spank
I
3

You may use the below command.

shopt -s expand_aliases

source ~/.bashrc

eval $command
Is answered 22/7, 2019 at 5:16 Comment(0)
R
1

If you execute it in a script, the alias will be over by the time the script finishes executing.

In case you want it to be permanent:

Your alias is well defined, but you have to store it in ~/.bashrc, not in a shell script.

Add it to that file and then source it with . .bashrc - it will load the file so that alias will be possible to use.

In case you want it to be used just in current session:

Just write it in your console prompt.

$ aa
The program 'aa' is currently not installed. ...
$ 
$ alias aa="echo hello"
$ 
$ aa
hello
$ 

Also: From Kent answer we can see that you can also source it by source your_file. In that case you do not need to use a shell script, just a normal file will make it.

Rung answered 12/4, 2013 at 9:46 Comment(4)
I dont want to store it in .bashrc. I need this alias to work only for current session.Zeena
Then just write it in your console prompt. If you execute it in a script, the alias will be over by the time the script finishes executing.Rung
If I have to set say 10 aliases all the time, may be writing in console prompt would be time consuming. Anyhow sourcing the script worked for me.Zeena
Yes, source from Kent answer makes it - I did not know it. What we can get from this is that you can even have a plain file (not .sh) with your alias stored and then sourcing them whenever you want to use.Rung
O
0

Your alias has to be in your .profile file not in your script if you are calling it on the prompt.

If you put an alias in your script then you have to call it within your script.

Source the file is the correct answer when trying to run a script that inside has an alias.

source yourscript.sh
Organizer answered 14/4, 2016 at 16:42 Comment(0)
T
0

Put your alias in a file call ~/.bash_aliases and then, on many distributions, it will get loaded automatically, no need to manually run the source command to load it.

Thiazine answered 19/5, 2019 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.