ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.