Linux - NewbieThis 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
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.
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.
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?
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
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.
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:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.