LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-05-2013, 07:09 PM   #1
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,366

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
append to an existing alias


I didn't think this would be so hard to find an answer to, but I'm coming up with nothing in my searches.

I simply want to append a flag onto whatever the existing alias for a command is.

For example
Code:
$ alias ls
alias ls='ls -d'
$ alias ls="ls -l"
$ alias ls
alias ls='ls -l'
What would like is for the second alias to simply append to the first, so the last output would be
Code:
alias ls='ls -d -l'
Is there any way to accomplish this? Some special flag in the alias command or some special manipulation of quotes in the call, so that it keeps the existing alias and simply adds to the end of it?
 
Old 11-05-2013, 07:45 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,655

Rep: Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256
don't think so.

You are asking an alias to work like a variable substitution, but that isn't what an alias is.

PS. it would also make it impossible to have different commands use the same utility in different ways:

alias ll="ls -l"
alias la="ls -a"

and the ever famous

alias ls="ls -ltr"

Last edited by jpollard; 11-05-2013 at 07:52 PM.
 
Old 11-05-2013, 08:07 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,366

Original Poster
Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
I'm not asking for the alias itself to function any different than it already does, I'm just asking for the alias program to expand the command you pass it (using any existing aliases) before setting the alias, rather than just blindly overwriting the alias with the literal string you pass it.

Once the alias is set, it would behave just like normal, it's just the setting of the alias that would be different.

If it's not possible, then I guess I'm going to have to do some ugly string manipulation to get that kind of functionality.
 
Old 11-05-2013, 08:22 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,655

Rep: Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256
Quote:
Originally Posted by suicidaleggroll View Post
I'm not asking for the alias itself to function any different than it already does, I'm just asking for the alias program to expand the command you pass it (using any existing aliases) before setting the alias, rather than just blindly overwriting the alias with the literal string you pass it.
Nope. Once you try to implement that you get instant recursion overflow trying to define an alias.
Quote:
Once the alias is set, it would behave just like normal, it's just the setting of the alias that would be different.
The problem is getting the definition right. Even if you limited it to a single interpretation pass.

alias ls="ls -ltr"

so how would you define

alias la="ls -a"

When it would always come out as "ls -ltr -a". You couldn't define the alias you wanted.
Quote:

If it's not possible, then I guess I'm going to have to do some ugly string manipulation to get that kind of functionality.
Not that ugly. You just precede the command you want with a $ - $ls, instead of just ls.

And it is done relatively frequently in complex shell scripts. The commands are defined as variables, with other variables used to specify options, then combined/merged in different ways to define specific
results. One common such variable is CC. $CC is the C compiler... except when it happens to be the C++ compiler instead.This is heavily done in Makefiles for instance - which pass the resulting command to the shell...
 
Old 11-05-2013, 08:35 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,366

Original Poster
Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
Quote:
Originally Posted by jpollard View Post
The problem is getting the definition right. Even if you limited it to a single interpretation pass.

alias ls="ls -ltr"

so how would you define

alias la="ls -a"

When it would always come out as "ls -ltr -a". You couldn't define the alias you wanted.
Except that's already exactly what happens.
If you run

alias ls="ls -ltr"
alias la="ls -a"

When you run "la", it's going to run "ls -ltr -a". I nest aliases like that all the time, works great. In fact that's the exact kind of behavior I'm looking for here, just without having to define a second command to get it.

If you wanted la to really mean "ls -a", with no prior aliases jumping into the mix, you would define it as

alias la="\ls -a"


Quote:
Originally Posted by jpollard View Post
Not that ugly. You just precede the command you want with a $ - $ls, instead of just ls.

And it is done relatively frequently in complex shell scripts. The commands are defined as variables, with other variables used to specify options, then combined/merged in different ways to define specific
results. One common such variable is CC. $CC is the C compiler... except when it happens to be the C++ compiler instead.This is heavily done in Makefiles for instance - which pass the resulting command to the shell...
I do things like that in shell scripts all the time too, unfortunately that won't work for this application.

Last edited by suicidaleggroll; 11-05-2013 at 08:37 PM.
 
Old 11-06-2013, 04:08 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,655

Rep: Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256
Oh well. Personally, I don't like aliases at all - I find it hides things I need to know, and causes problems when they disappear for some reason - it ties you to a specific command interpreter.
 
  


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
Append date to existing file name? miros84 Linux - Software 8 02-13-2013 02:56 AM
how to append in a existing file descriptor in linux and also insert a new line. mot12 Programming 4 08-30-2011 08:48 AM
Kate: Append files to existing list and enable the Recent List AGazzaz Linux - Software 2 10-13-2009 09:27 PM
how to append contents of one file to another existing file amit_pansuria Programming 1 11-17-2008 11:01 AM


All times are GMT -5. The time now is 01:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration