36

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 ?

3
  • 2
    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.) Commented Sep 12, 2019 at 10:17
  • Why doesn't my Bash script recognize aliases? Commented Sep 12, 2019 at 10:19
  • 1
    The alias was defined in the shell that executed the script, not the shell from which you executed the script. Commented Jan 19, 2023 at 14:17

7 Answers 7

56

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Problem of this approach is that all the script variable will invade your environment. which is not what you want!
42

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

2 Comments

OP isn't trying to use the alias in the script, but after the script has completed.
@chepner I don't think this is specified either way in this question. It is though in the link to the "duplicate", which may therefore not be a duplicate after all...
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 

3 Comments

And how does executing alias xyz="cd /home/usr/src/xyz" add to ~/.bashrc?
Sorry, but for me the $alias, $unalias and $source make no sense. Why are they variables? Unless you explain that, I downvote.
$ 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.
3

You may use the below command.

shopt -s expand_aliases

source ~/.bashrc

eval $command

Comments

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.

4 Comments

I dont want to store it in .bashrc. I need this alias to work only for current session.
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.
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.
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.
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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.