-   Linux - Newbie (
-   -   No script output for command alias (

mikenash 02-24-2016 07:56 AM

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.

rtmistler 02-24-2016 08:04 AM

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?

BW-userx 02-24-2016 12:14 PM

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

pan64 02-24-2016 12:25 PM
why doesn't my bash script recognize aliases
how do i create a permanent bash alias

mikenash 02-24-2016 02:42 PM

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.

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
echo alias
echo 'alias'
echo "alias"
ls -l
Wed Feb 24 2016 03:37 PM
root@linux234:/root $~>./test_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

rtmistler 02-24-2016 02:48 PM



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.

pan64 02-25-2016 01:37 AM

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

#!/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:

source test_alias
if you want to see how your script works insert set -xv at the beginning of it:

set -xv

mikenash 02-25-2016 12:30 PM

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.

pan64 02-25-2016 12:42 PM

the child process will inherit part of the environment, but especially the alias settings are not inherited.

rknichols 02-25-2016 04:31 PM

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.

mikenash 02-26-2016 07:29 AM

Thank you to everyone for sharing your knowledge.

pan64 02-26-2016 10:10 AM

(if you really want to say thanks just click on yes)

All times are GMT -5. The time now is 02:50 PM.