LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-24-2016, 08:56 AM   #1
mikenash
Member
 
Registered: Dec 2014
Posts: 84

Rep: Reputation: Disabled
No script output for command alias


Executing alias from the command line produces the expected output but when used within a script there is no output. Tried with no quotes and with single ' and double " quotes. Commands like ls -l and pwd work fine.
 
Old 02-24-2016, 09:04 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,445
Blog Entries: 13

Rep: Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762
I always get confused about what environment exists within a script.

Plus there's also ways to import an environment to a script.

The other issue is who owns the script and who runs it.

In multi-user domains this has been run into, in the past where someone wrote a script everyone uses, and then something changes, that original user leaves the company, or whatever, and suddenly a long-time known command doesn't work and we all have to scramble.

My end result is that I use absolute path names in my scripts and do not rely on aliases in a script.

Another way to think of this is that a script is already an automatic way to do stuff, so why cut corners to save typing or something by using an alias?
 
Old 02-24-2016, 01:14 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,634

Rep: Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074
where is your code so we can then have a better understanding of what you actually did or are trying to do?
use code tags

Adding tags to your post

Last edited by BW-userx; 02-24-2016 at 01:16 PM.
 
Old 02-24-2016, 01:25 PM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,611

Rep: Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365
http://www.tldp.org/LDP/abs/html/aliases.html
why doesn't my bash script recognize aliases
how do i create a permanent bash alias

Last edited by pan64; 02-25-2016 at 02:23 AM.
 
Old 02-24-2016, 03:42 PM   #5
mikenash
Member
 
Registered: Dec 2014
Posts: 84

Original Poster
Rep: Reputation: Disabled
First I execute alias on the command line and I see the expected output.
Then I display the test_alias script.
Then I execute it.
I receive out put from ls and pwd but none from alias.
Code:
root@linux234:/root $~>alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
Wed Feb 24 2016 03:36 PM
root@linux234:/root $~>cat test_alias
#!/bin/bash
alias
'alias'
"alias"
echo alias
echo 'alias'
echo "alias"
ls -l
pwd
Wed Feb 24 2016 03:37 PM
root@linux234:/root $~>./test_alias
alias
alias
alias
total 20
-rw-r--r--. 1 root root  112 Oct 28 09:35 2
-rw-------. 1 root root 1949 Aug 12  2015 anaconda-ks.cfg
-rw-r--r--  1 root root    0 Feb 16 09:29 cat
-rw-r--r--  1 root root    0 Jan 22 10:14 date
drwxr-xr-x. 2 root root    6 Aug 12  2015 Desktop
drwxr-xr-x. 2 root root    6 Aug 12  2015 Documents
drwxr-xr-x. 2 root root    6 Aug 12  2015 Downloads
-rw-r--r--. 1 root root 1997 Aug 12  2015 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Aug 12  2015 Music
drwxr-xr-x. 2 root root    6 Aug 12  2015 Pictures
drwxr-xr-x. 2 root root    6 Aug 12  2015 Public
drwxr-xr-x. 2 root root    6 Aug 12  2015 Templates
-rw-r--r--  1 root root   15 Jan 23 16:50 test
-rwxr-xr-x  1 root root   81 Feb 24 08:55 test_alias
-rw-r--r--  1 root root    0 Jan 22 10:14 uname
-rw-r--r--  1 root root    0 Jan 22 10:14 uptime
drwxr-xr-x. 2 root root    6 Aug 12  2015 Videos
/root
 
Old 02-24-2016, 03:48 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,445
Blog Entries: 13

Rep: Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762
Code:
#!/bin/bash

alias &> alias.txt
Outside of your script you can cat the alias.txt file to see what your aliases are.

Or you can assign the output of the alias command to a variable and then echo that variable. I don't do that much because it's a general string.

Once again, I do not recommend using aliases within a script, instead invoke the fully qualified command. But what I'm seeing is that you're just trying to view what your aliases are.
 
Old 02-25-2016, 02:37 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,611

Rep: Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365
I edited my previous post to see what are those pages talking about. I think the answer(s) to your questions are there, you just need to read them.
But as far as I see you did not ask anything, you did not request anything, you posted only statements. So please tell us what do you want to really achieve, what do you expect to happen and how did you try it to solve, what's happened instead...

From your last post I can draw some conclusion:
cat test_alias prints the content of the file test_alias
Code:
#!/bin/bash                    # this is called shebang, used to specify the application/interpreter to execute the actual script
alias                          # this is the command alias to print the aliases defined in the current shell
'alias'                        # this is the same again
"alias"                        # and again
echo alias                     # this will print the word alias
echo 'alias'                   # this is the same echo
echo "alias"                   # and again
ls -l                          # this will list the content of the current dir
pwd                            # print working/current directory
you posted the result and you can see the first 3 alias commands printed nothing. Remember, when you execute this (any) script it will run in another shell and you can only see the aliases inside that shell. I posted links about how to solve this issue.

If you want to execute your script in the current, actual shell you must type:
Code:
source test_alias
if you want to see how your script works insert set -xv at the beginning of it:
Code:
#!/bin/bash
set -xv
...
 
Old 02-25-2016, 01:30 PM   #8
mikenash
Member
 
Registered: Dec 2014
Posts: 84

Original Poster
Rep: Reputation: Disabled
So, if I understand this correctly, the reason there is no output from the test_alias script is because it has it's own environment and does not inherit the current shell's environment. The source command will execute commands from the script in the current environment. Reviewing the docs I am not finding any option/parameter/command to inherit the previous environment from within the script. Can anyone verify this as true or provide an option/parameter/command where the script will inherit the parent's environment.
 
Old 02-25-2016, 01:42 PM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,611

Rep: Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365
the child process will inherit part of the environment, but especially the alias settings are not inherited.
 
Old 02-25-2016, 05:31 PM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,362

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
Your script is executed in a non-interactive subshell, and aliases are not expanded when the shell is not interactive unless that shell has the expand_aliases shell option set. If it were otherwise, then writing shell scripts for general use would be impossible since no command could be relied upon to not be an alias for something completely different.
 
Old 02-26-2016, 08:29 AM   #11
mikenash
Member
 
Registered: Dec 2014
Posts: 84

Original Poster
Rep: Reputation: Disabled
Thank you to everyone for sharing your knowledge.
 
Old 02-26-2016, 11:10 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,611

Rep: Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365Reputation: 4365
(if you really want to say thanks just click on yes)
 
  


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
sourced script won't execute alias after command expansion NeoLux Programming 4 03-08-2015 12:05 AM
Any Linux command output to delimited format. Script should work for any command. torrelm@hotmail.com Linux - Newbie 9 09-04-2014 09:54 AM
bash script to display alias commands and un-alias any less than 12 characters bani Linux - Newbie 5 01-19-2014 01:34 PM
Calling alias command in script snowman81 Linux - Newbie 4 04-06-2008 03:39 PM
A command or script to rm files based on df output? Anithen Slackware 7 06-21-2007 11:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:59 PM.

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