LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2013, 04:40 AM   #1
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Rep: Reputation: Disabled
Is aliases available in shell scripts?


I define aliases and save in rc file. what I see is that it isn't available in shell scripts. They are however available in interpretive or interactive sub-shell which I get when I give command
Code:
bash
For ex.:
Code:
I define 
alias echo='echo -n'
it's unavailable in shell scripts but sub-shells.
 
Old 04-17-2013, 04:46 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Storing your aliases in an rc file (which one?) is not the way to go.

If you want to make them global I would suggest putting them in /etc/profile (or /etc/profile.d/). If you need them locally store them in ~/.profile (or ~/.bash_aliases).
 
Old 04-17-2013, 04:54 AM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Also, expansion of aliases is usually disabled in non-interactive shells (scripts).
You can put
Code:
shopt -s expand_aliases
in your script to enable them.
 
Old 04-17-2013, 05:56 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
It's better to use functions than aliases.

And if you're wrapping a builtin, you could use builtin to call the builtin.
Code:
function echo {
    builtin echo -n "$@"
}
^ Although replacing echo with such default functionality is not recommended.
 
Old 04-17-2013, 09:31 AM   #5
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Original Poster
Rep: Reputation: Disabled
konsolebox, please elaborate. What you mean by builtin
 
Old 04-17-2013, 10:01 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Oh yeah I didn't check. It's a feature in bash, and I'm referring to bash builtins. Run 'help builtin' if you're in bash. And run 'help' without any argument to see the list of those.

See the Bash manual for more info.
 
Old 04-18-2013, 03:14 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Aliases are not enabled by default in shell scripts. As mentioned, they can be enabled with shopt, but even then that only makes it possible to create them in the script. You won't get immediate access to all the aliases in the main shell. Since aliases can't be exported, there's no way for a sub-process like a script to inherit a copy of them.

Aliases are rather pointless to use inside scripts anyway. Since you only need to write each command once, there's no real need for such user-interactive style shortcuts. It would also tend to make the code less obvious.

As mentioned, if you need to define code that will be used multiple times, or want to simplify longer commands, use functions.

BTW, if you just want to shorten up a longer command that has regularly-defined option strings, you can store them in an array first.

Code:
options=( -x -n -a foobar --long-option=bazbum )
mycommand "${options[@]}" filename.txt
As for "echo -n", I'd recommend using printf instead, whenever you need anything fancier than the default echo.

Last edited by David the H.; 04-18-2013 at 03:17 PM. Reason: whoops, gave wrong advice
 
  


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
aliases with bash shell edccmu22 Linux - Newbie 3 11-04-2007 11:59 AM
Useful shell aliases exit3219 Linux - General 14 03-16-2007 04:18 AM
Using shell aliases in make Dark_Helmet Programming 13 02-26-2007 03:16 AM
aliases and the 'sh' shell koodoo Linux - Newbie 14 08-05-2006 04:19 AM
Comparison of shell functions and aliases? slakmagik Programming 3 02-07-2004 05:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:16 AM.

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