Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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 have been fiddling around a bit for something I tried to use many times, and it never worked (I manually expanded the aliases, where I used them). Today there was a more tries, but still nothing so clear for the problem.
Basic idea: want to use my alias on my shell scripts.
Here is some output that might be a more informative example than long paragraphs:
Code:
$ cat ~/.bashrc
# ...
# ONLY some aliases [trimmed from this output]
# ...
alias dddttt='"date" "+%F %T"'
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# (above: infamous line I took long to read about or to note it)
# ...
# more aliases, functions and so on
# ...
$ cat ~/bomb.sh
#!/bin/bash
# date "+%F %T"
dddttt
$ bash
$ ~/bomb.sh
/home/user/bomb.sh: line 3: dddttt: command not found
So, why it still don't work?? I have moved my aliases above that "infamous" line. It makes sense to work. Doesn't it!?
Last edited by dedec0; 08-05-2015 at 07:37 PM.
Reason: clearing up
Because Bash does not expand aliases unless the shell is interactive (by default). Can you imagine what could happen if scripts had the commands they were expecting to run changed by aliases? There is an option to override this, but you would probably be better off not using aliases in shell scripts.
But I would really like to use my aliases in my shell scripts. Some of them are more complex and big than what I've given here as an example. Maybe I'll even turn on my functions too, since they should not conflict with any program name, installed or not.
Is the option shopt -e expand_aliases ? Can I put it inside my scripts? Or just in bashrc (which is not that good) ?
But I would really like to use my aliases in my shell scripts. Some of them are more complex and big than what I've given here as an example. Maybe I'll even turn on my functions too, since they should not conflict with any program name, installed or not.
Is the option shopt -e expand_aliases ? Can I put it inside my scripts? Or just in bashrc (which is not that good) ?
For me mainly aliases are there to either simplify typing, or put in stuff which I'd normally not think to add, such as the --color for the ls command.
Variables in bash defined in the script are there for the duration of the script. So if you say something like:
Code:
#!/bin/sh
# This is my script
LS='/bin/ls --color=auto'
And then you use $LS within your script, you are thus using an alias.
I knew I can use variables to execute anything. But I want to use the already defined aliases in my scripts - so they're much easier to write and to also understand them, for whoever reads it. The useful aliases are in bashrc, so they will be defined there, and not in the script itself, like that $LS.
Anyway, later today I'll try something I have just found:
#!/bin/bash
# alias.sh
# Must set this option, else script will not expand aliases.
shopt -s expand_aliases
alias Jesse_James='echo "\"Alias Jesse James\" was a 1959 comedy starring Bob Hope."'
Jesse_James
echo; echo; echo;
# [...]
I knew I can use variables to execute anything. But I want to use the already defined aliases in my scripts - so they're much easier to write and to also understand them, for whoever reads it. The useful aliases are in bashrc, so they will be defined there, and not in the script itself, like that $LS.
Anyway, later today I'll try something I have just found:
#!/bin/bash
# alias.sh
# Must set this option, else script will not expand aliases.
shopt -s expand_aliases
alias Jesse_James='echo "\"Alias Jesse James\" was a 1959 comedy starring Bob Hope."'
Jesse_James
echo; echo; echo;
# [...]
I don't think that's a good idea. You write something using particular definitions only known to your login and then distribute it to other people, there may be problems with privileges, as well as future problems when you change things.
Making it easier is not about dumbing it down, but instead providing good structure, comments, and educating the audience if they still can't follow your code.
In a script, aliases have very limited usefulness. (...) Bash does not expand arguments within the alias body. Moreover, a script fails to expand an alias itself within "compound constructs," such as if/then statements, loops, and functions. An added limitation is that an alias will not expand recursively. Almost invariably, whatever we would like an alias to do could be accomplished much more effectively with a function.
but i think i already know what you're going to say to that:
"But I really, really want to use aliases in my scripts..."
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.