Forum     

Go Back   Digit Technology Discussion Forum > Software > Open Source
Register FAQ Calendar Mark Forums Read

Open Source A place where you can talk to like-minded people about the fastest growing software movement today! Discuss anything and everything about Open Source software and Operating Systems.


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 20-01-2006, 11:16 AM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Jan 2006
Posts: 5
Default Alias Does Not Work In Shell Script


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?
mannahazarika is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 20-01-2006, 08:46 PM   #2 (permalink)
left this forum longback
 
praka123's Avatar
 
Join Date: Sep 2005
Location: -
Posts: 7,536
Default Re: Alias Does Not Work In Shell Script

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:~$
praka123 is offline  
Old 24-01-2006, 05:29 PM   #3 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
Default Re: Alias Does Not Work In Shell Script

Quote:
Originally Posted by mannahazarika
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?"
mediator is offline  
Old 24-01-2006, 05:48 PM   #4 (permalink)
Alpha Geek
 
Join Date: Dec 2003
Posts: 780
Default

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.
__________________
Hardware - Celeron 266 MHz, 128MB RAM, Intel i740 8MB RAM
Software - Slackware Linux 10.1; Archlinux 0.7; Windows 98 SE
Humanware - GS/CS C++ L+++ w b+++ DI+ D+ e
ujjwal is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by icebags
- by clinton
- by topgear
- by trublu

Advertisement




All times are GMT +5.5. The time now is 10:17 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2