Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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 am working on Debian Stable Linux which is otherwise working very well. I find that 'alias' command works on terminal but does not work when it is kept in a script file:
Code:
auser@debian:~$ cat findalias.sh
#! /bin/bash
alias
auser@debian:~$ ./findalias.sh
auser@debian:~$
Where is the problem and how can it be solved? Thanks for your help.
You need to remove space character between '#!' and '/bin/bash'
Corrected. It still does not work.
Adding or removing the space does not matter. In following version, "echo" command works but not "alias" command whether or not there is a space between "#!" and "/bin/bash":
I always forget if you have a default environment or your same environment when you enter a script
Use set -xv as the second line of your script to enable verbose output and debugging to see more details
Check to verify you actually have any aliases. And then this circulates back to the question about what your environment is when you enter a script (I'm web searching that now)
My read from brief research is that environment variables do not get imported to a script, they have to be exported specifically for the script to be able to see them. Meanwhile aliases are environment variables. I think within your aliases file you can choose to put "export" prior to each line defining the alias.
Above works perfectly. Thanks. It will be useful if you can explain when and why -i should be used. I checked with "man bash" command and it is mentioned to be for 'interactive' mode. Could not really understand.
you can choose to put "export" prior to each line defining the alias.
Writing "export" at start of alias line in .bashrc file actually makes alias not available. That particular alias does not work while alias lines where export is not mentioned works. I checked from a new terminal after editing .bashrc file.
For safety reason bash in script mode does not read the .bashrc in the homedirectory of the user who starts it.
But -i, interactive mode, enforces that.
You can consider bash -i unsafe but convenient in certain cases.
The export of aliases and functions (to environment), as mentioned by rtmistler, is specific to bash, does not work on some non-Linux platforms, and bares risks. This environment looks strange and is inherited by all programs that the script runs.
And it is not so simple to use.
I consider this bash feature unnecessary and generally do not recommend it.
Writing "export" at start of alias line in .bashrc file actually makes alias not available. That particular alias does not work while alias lines where export is not mentioned works. I checked from a new terminal after editing .bashrc file.
Just curious, rng, what are you trying to accomplish by executing the alias command in a script?
I have created many aliases (and forget them) and I use this script to find them by pattern:
Code:
alias | grep $1
So if I had created an alias for "grep -i --color='always'", I can search for it by giving command "findalias grep". It is really more for experimenting with terminal.
I am still not clear why all other bash commands work in scripts but not alias. I never had to use '-i' switch over last many years that I have been using Linux regularly.
I am still not clear why all other bash commands work in scripts but not alias. I never had to use '-i' switch over last many years that I have been using Linux regularly.
The alias command works fine in scripts, and that's with or without "-i". It's just that aliases are not exported, so your script starts out with no aliases defined. Unless your script sources your .bashrc or other file containing alias definitions there won't be any output.
Aliases are not exported because, by default, aliases are not expanded in non-interactive shells. You would have to set the expand_aliases shell option ("shopt -s expand_aliases") in such a shell in order to make use of aliases.
I have created many aliases (and forget them) and I use this script to find them by pattern:
Code:
alias | grep $1
So if I had created an alias for "grep -i --color='always'", I can search for it by giving command "findalias grep". It is really more for experimenting with terminal.
OK. Makes sense, except that 1.
Code:
alias | grep grep
would always work, and is only four more keystrokes than using the script and you don't have to maintain and remember that the script is there.
Of course, then we wouldn't have had the opportunity for this most interesting discussion and larnin'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.