LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 12-20-2017, 07:33 AM   #1
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,077

Rep: Reputation: 43
Why "alias" does not work from script file


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.
 
Old 12-20-2017, 08:36 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,229

Rep: Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711
You need to remove space character between '#!' and '/bin/bash'
 
Old 12-20-2017, 10:01 AM   #3
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,077

Original Poster
Rep: Reputation: 43
Quote:
Originally Posted by keefaz View Post
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":

Code:
$ cat ./findalias.sh 
#! /bin/bash
echo "testing"
alias

auser@debian:~$ ./findalias.sh 
testing
auser@debian:~$ cat ./findalias.sh 
#!/bin/bash
echo "testing"
alias

auser@debian:~$ ./findalias.sh 
testing
auser@debian:~$

Last edited by rng; 12-20-2017 at 10:06 AM.
 
Old 12-20-2017, 10:27 AM   #4
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 870

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Run your script with
Code:
bash -i /path/to/script
Or change the shebang to
Code:
#!/bin/bash -i
If you want to see how it initializes, run it with
Code:
bash -ix /path/to/script
 
1 members found this post helpful.
Old 12-20-2017, 10:28 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,051
Blog Entries: 12

Rep: Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590
Few things to consider:
  • 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)
 
Old 12-20-2017, 10:30 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,051
Blog Entries: 12

Rep: Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590Reputation: 2590
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.
 
Old 12-20-2017, 11:17 AM   #7
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,077

Original Poster
Rep: Reputation: 43
Quote:
MadeInGermany wrote:

Or change the shebang to
Code:
#!/bin/bash -i
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.

Last edited by rng; 12-20-2017 at 11:20 AM.
 
Old 12-20-2017, 12:00 PM   #8
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,077

Original Poster
Rep: Reputation: 43
Quote:
rtmistler wrote:

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.
 
Old 12-20-2017, 12:14 PM   #9
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 870

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
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.
 
Old 12-20-2017, 12:48 PM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,229

Rep: Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711
Quote:
Originally Posted by rng View Post
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.
Maybe source bashrc file in your script?
 
1 members found this post helpful.
Old 12-20-2017, 01:25 PM   #11
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 1,612

Rep: Reputation: 528Reputation: 528Reputation: 528Reputation: 528Reputation: 528Reputation: 528
sourcing .bashrc within the script would have the same effect as using the -i switch, with the same reservations as expressed by MadeInGermany.

Just curious, rng, what are you trying to accomplish by executing the alias command in a script?
 
Old 12-20-2017, 08:02 PM   #12
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,077

Original Poster
Rep: Reputation: 43
Quote:
scasey wrote:

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.

Last edited by rng; 12-20-2017 at 08:06 PM.
 
Old 12-20-2017, 08:44 PM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,986

Rep: Reputation: 1755Reputation: 1755Reputation: 1755Reputation: 1755Reputation: 1755Reputation: 1755Reputation: 1755Reputation: 1755Reputation: 1755Reputation: 1755Reputation: 1755
Quote:
Originally Posted by rng View Post
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.
 
Old 12-21-2017, 12:50 PM   #14
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 1,612

Rep: Reputation: 528Reputation: 528Reputation: 528Reputation: 528Reputation: 528Reputation: 528
Quote:
Originally Posted by rng View Post
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'
 
Old 12-21-2017, 01:12 PM   #15
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 870

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Where you define your aliases you can define a function
Code:
findalias(){
alias | grep "$1"
}
A function has the same scope as an alias.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the purpose of "prologue" field used by kernel module signing script "sign-file" manisha.jnu08 Linux - Kernel 1 12-13-2017 09:48 AM
[SOLVED] "ifconfig eth0 up" doesn't work, cause of "no such file or directory" Sebi94 Linux - Networking 6 08-21-2012 10:39 PM
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
Problem using "newaliases" to update sendmail alias file davidks1977 Linux - Software 4 03-17-2004 11:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration