LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-19-2006, 02:44 AM   #1
mannahazarika
Member
 
Registered: Dec 2005
Location: Dibrugarh, Assam, India
Distribution: Fedora, Debian, Suse
Posts: 40

Rep: Reputation: 15
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?


-----------------
Makarand Hazarika
NIT Durgapur

Last edited by mannahazarika; 02-24-2006 at 02:56 AM.
 
Old 01-19-2006, 08:50 AM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
That's funny... your sample works perfectly on my machine.
 
Old 01-19-2006, 09:33 PM   #3
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis
Posts: 159
Blog Entries: 1

Rep: Reputation: 18
Doesn't on my system. (???) I, too, would like answer to this.

Also, when i set an environment variable within a bash script, and 'export' the variable, it only exists as long as the script is running. Are these related symptoms?

Last edited by dogpatch; 01-19-2006 at 09:35 PM.
 
Old 01-19-2006, 09:59 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
Quote:
Originally Posted by dogpatch
Also, when i set an environment variable within a bash script, and 'export' the variable, it only exists as long as the script is running. Are these related symptoms?
According to http://www.tldp.org/LDP/abs/html/abs...html#EXPORTREF, that's normal behaviour. The variables are available in the current shell (and sub-processes) but not to the parent.
 
Old 01-19-2006, 10:15 PM   #5
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis
Posts: 159
Blog Entries: 1

Rep: Reputation: 18
Thanks for the link, gilead. I will use that as a reference to hopefully get up to speed on scripting. Maybe i'll also find an answer there to the original alias question that mannahazarika posed.
 
Old 01-19-2006, 10:32 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
For reference, I am running:
Code:
$ bash --version
GNU bash, version 3.00.16(1)-release (i686-pc-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.
... and oddly enough, I now have that behavior. Alias doesn't seem to work. I'm not sure why it was working before, or what I did wrong. Oddly enough, adding a line with 'alias' on it in that shell DOES show the alias being set.
 
Old 01-19-2006, 10:35 PM   #7
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
And, the key point is the shell option "expand_aliases". (bash(1)) The following code does work:
Code:
#!/bin/bash
shopt -s expand_aliases
alias hi='echo hello world'
hi
 
Old 01-19-2006, 11:14 PM   #8
mannahazarika
Member
 
Registered: Dec 2005
Location: Dibrugarh, Assam, India
Distribution: Fedora, Debian, Suse
Posts: 40

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Matir
... and oddly enough, I now have that behavior. Alias doesn't seem to work. I'm not sure why it was working before, or what I did wrong. Oddly enough, adding a line with 'alias' on it in that shell DOES show the alias being set.
I am sorry but I did not understand exactly what you meant. Could you please explain it?



-----------------
Makarand Hazarika
NIT Durgapur

Last edited by mannahazarika; 02-24-2006 at 02:58 AM.
 
Old 01-20-2006, 11:04 AM   #9
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
Well, to make a long story short, the 'expand_aliases' shell option must be turned on for it to evaluate aliases that have been set. I would've thought that using the 'alias' command would enable expand_aliases, but apparently not.
 
Old 01-20-2006, 01:56 PM   #10
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
This is also affected by how the script is run. If the shopt option had already been set in the current shell, its value would be accessible if the script containing the alias were run as:

Code:
source scriptname
However the current shell's shopt option is not available (and the shopt statement needs to be in the script as per Matir's example) when the script is run as:

Code:
./scriptname
The reason for this is that using the source command runs the script in the current shell and gives it access to the current execution environment. However, executing the script standalone (./scriptname) creates a subshell which only inherits a subset of the current shell's environment.

The Command Execution Environment section of `man bash` describes what the environment consists of and how much of it is passed to subprocesses.

This doesn't add much to the discussion, but I found it interesting reading
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
my first shell script doesnt work :( boxerboy Programming 16 12-22-2005 06:31 PM
why won't my shell script work :( Sanji Linux - Software 1 04-12-2005 08:10 PM
Delay in shell script or alias jeopardyracing Linux - Newbie 6 09-21-2004 08:07 PM
A C-shell script doesn't work on redhat 7.3 but works on Sun Solaris Belmer Linux - Newbie 2 02-08-2004 11:05 AM
Shell script won't work. Why? bruno buys Linux - Newbie 2 11-16-2003 07:28 PM


All times are GMT -5. The time now is 09:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration