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


Reply
  Search this Thread
Old 08-05-2015, 07:36 PM   #1
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,358

Rep: Reputation: 51
Question aliases and bash scripts


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
 
Old 08-05-2015, 07:42 PM   #2
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,358

Original Poster
Rep: Reputation: 51
Arrow Maybe useful

Some of you may find this page useful. For me, it is kind of confuse, mostly - although it showed me the "infamous line".

Some people around here you might use/understand it better than myself:

http://xpt.sourceforge.net/techdocs/...h/ar01s13.html
 
Old 08-05-2015, 07:44 PM   #3
ljb643
Member
 
Registered: Nov 2003
Posts: 524

Rep: Reputation: Disabled
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.
 
Old 08-06-2015, 09:29 AM   #4
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,358

Original Poster
Rep: Reputation: 51
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) ?
 
Old 08-06-2015, 09:39 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,841
Blog Entries: 13

Rep: Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894
Quote:
Originally Posted by dedec0 View Post
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.
 
Old 08-06-2015, 11:35 AM   #6
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,358

Original Poster
Rep: Reputation: 51
Thumbs down

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:

http://www.tldp.org/LDP/abs/html/aliases.html

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

# [...]
 
Old 08-06-2015, 12:54 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,841
Blog Entries: 13

Rep: Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894Reputation: 4894
Quote:
Originally Posted by dedec0 View Post
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:

http://www.tldp.org/LDP/abs/html/aliases.html

Code:
#!/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.
 
Old 08-06-2015, 06:17 PM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052
Quote:
Originally Posted by dedec0 View Post
it says so right there:
Quote:
Originally Posted by tldp
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..."

well, why not, go ahead.
 
Old 08-08-2015, 04:43 PM   #9
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,358

Original Poster
Rep: Reputation: 51
That's bad. I didn't read all that document... (just tired).

Script to convert all aliases to functions, activate!

LOL
 
  


Reply

Tags
alias, bash, bashrc, script, shell


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
Is aliases available in shell scripts? ravisingh1 Programming 6 04-18-2013 04:14 PM
LXer: Python Scripts as a Replacement for Bash Utility Scripts LXer Syndicated Linux News 1 01-17-2013 09:08 AM
[SOLVED] Bash Aliases Not Working schachwizard Linux - Newbie 6 10-12-2010 05:21 PM
aliases with bash shell edccmu22 Linux - Newbie 3 11-04-2007 12:59 PM
Bash aliases in OS X?? Grife Other *NIX 3 03-14-2007 07:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:53 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
Open Source Consulting | Domain Registration