LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 10-14-2008, 05:17 AM   #1
SwingingSimian
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Rep: Reputation: 0
BASH alias scoping


Hi all

I'm trying to use aliases within function but am having problems like:

bash: arrayshome: command not found

The alias 'arrayshome' is available via the command line, but not when I call a function which uses it. Can someone clarify the scoping behaviour of aliases in BASH, as I've so far only found general comments like this:

"Bash uses the POSIX.2 scoping rules, which make the function execution environment an exact copy of the shell environment with the replacement of the shell's positional paramters with the function arguments."

Can someone clarify this? I just want to know whether I need to code around it, or whether there is something funky going on, as it looks to me like this should work.

Thanks

Nath
 
Old 10-14-2008, 05:43 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
You can use aliases within a function.
 
Old 10-14-2008, 06:17 AM   #3
SwingingSimian
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Yes, that's what I thought.

It turns out that it loses scope if you try and run it via a function sourced from a different file.

So I have a general functions file which I source at the start of all of my scripts. This contains an Execute function which catches errors. I was calling:

Execute myalias

This was causing the errors, but reverting to just the command and implementing error catching in the caller works fine.

A slight pain, but easily avoided, if a little verbose.

Ta

Nath
 
Old 10-14-2008, 06:41 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
An excerpt from the Advanced Bash Scripting Guide
Quote:
In a script, aliases have very limited usefulness. It would be quite nice if aliases could assume some of the functionality of the C preprocessor, such as macro expansion, but unfortunately Bash does not expand arguments within the alias body. [1] Moreover, a script fails to expand an alias itself within "compound constructs," such as if/then statements, loops, and functions. An added limitation is that an alias will not expand recursively. Almost invariably, whatever we would like an alias to do could be accomplished much more effectively with a function.
you can try to transform your aliases into functions and see if they work the way you expect.
 
  


Reply



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
bash login sourcing .alias ned737 SUSE / openSUSE 2 11-17-2005 02:03 AM
bash ls --color alias? tripmix Linux - Newbie 7 01-18-2005 04:49 AM
alias in bash shell wincrk Linux - General 12 07-20-2004 12:35 PM
A bash alias question CodeWarrior Slackware 11 06-03-2003 07:09 PM
bash and alias kleptos Slackware 6 01-23-2003 08:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:37 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration