DIGIT-ARC read-only archive · snapshot: July 23, 2026

Alias Does Not Work In Shell Script

4 posts · originally from https://geek.digit.in/community/threads/alias-does-not-work-in-shell-script.22426/

M
mannahazarika Original post #1
Jan 20, 2006 at 11:16 AM
When I tried to use alias in shell script, it didn't work.

This will clearify what I mean:-

#!/bin/bash

alias hello='echo Hello, how are you?'

hello

#end of shell script

If this shell script is run it will give an error message stating that the command hello was not found.

Why does this happen? Wasn't the alias command executed? Then why hello was not aliased?
praka123
Jan 20, 2006 at 8:46 PM
but you can have it run on sh?
Code:
prakash@suryan:~/Desktop$ vi hello
prakash@suryan:~/Desktop$ sh hello
Hello,How are You?


this will be the soltn..
Code:
prakash@suryan:~$ vi Desktop/hello
#! /bin/bash

shopt -s expand_aliases

alias hello='echo Hello,How are You?'
hello


echo


exit
prakash@suryan:~$ Desktop/hello
Hello,How are You?

prakash@suryan:~$
:)
mediator
Jan 24, 2006 at 5:29 PM
mannahazarika said:
When I tried to use alias in shell script, it didn't work.

This will clearify what I mean:-

#!/bin/bash

alias hello='echo Hello, how are you?'

hello

#end of shell script

If this shell script is run it will give an error message stating that the command hello was not found.

Why does this happen? Wasn't the alias command executed? Then why hello was not aliased?

The syntax is wrong dood!
try as..
alias hello='echo "Hello, how are you?"'
then try hello

the syntax between ' ' is a shell command in ur case is echo "Hello, how are you?"
U
ujjwal #4
Jan 24, 2006 at 5:48 PM
You need to add this line to your script to make bash expand aliases in it -

Code:
shopt -s expand_aliases

mediator, that should not matter, echo echoe's whatever arguments is provided to it, whether quoted together or not.