Alias Does Not Work In Shell Script

Status
Not open for further replies.

mannahazarika

Right off the assembly line
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

left this forum longback
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

Technomancer
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?"
 

ujjwal

Padawan
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.
 
Status
Not open for further replies.
Top Bottom