LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   append to an existing alias (https://www.linuxquestions.org/questions/linux-newbie-8/append-to-an-existing-alias-4175483515/)

suicidaleggroll 11-05-2013 06:09 PM

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?

jpollard 11-05-2013 06:45 PM

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"

suicidaleggroll 11-05-2013 07:07 PM

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.

jpollard 11-05-2013 07:22 PM

Quote:

Originally Posted by suicidaleggroll (Post 5058999)
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...

suicidaleggroll 11-05-2013 07:35 PM

Quote:

Originally Posted by jpollard (Post 5059007)
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 (Post 5059007)
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.

jpollard 11-06-2013 03:08 AM

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.


All times are GMT -5. The time now is 11:19 AM.