1

I have 2 separate files, which are both sourced from my .zshrc, one of them (the first one) is defining this alias:

alias wget='wget --hsts-file="$XDG_CACHE_HOME/wget-hsts"'

problem is that the other one, sourced after this one is also defining wget alias:

alias wget='wget -c'

I need to somehow ensure, that the second alias defines itself using the previous alias. I was thinking of using something like $(wget), but that just gets me into a recursive loop. Is there some good way to reference an already existing alias with the same name while defining a new one?

I was maybe thinking of somehow getting the content of the first alias and only concatenating that with my new flag so that it will build on the previous alias, I'd probably also have to do some handling to fall back to the real command if the command wasn't already aliased before.

Note: I know that I could just change the name of one of those aliases and reference it in the next one, but that's not what I'm asking for. Nor do I want to just simply define them as 1 alias manually. I want a way to actually automatically use the old alias definition when defining a new one, so that they get chained together .

Any idea how could I implement something like this? Or perhaps a better method to do something like this?

6
  • 3
    you tagged with Bash but mention zsh, what do shell do you actually use? Commented Jan 5, 2021 at 14:50
  • @ArkadiuszDrabczyk I use zsh, but it shouldn't matter, I think the aliases behave in the same way in both, I might be wrong but I'm not aware of any difference
    – ItsDrike
    Commented Jan 5, 2021 at 14:55
  • @KamilMaciorowski I've changed the tags, and what I meant was that I didn't want the first alias to just be directly combined with the second manually, by specifying both tags in a single alias, but rather a method of doing this automatically
    – ItsDrike
    Commented Jan 7, 2021 at 0:07
  • @KamilMaciorowski yes, I've adjusted the question too, it should be clearer that they should be automatically merged into a single alias.
    – ItsDrike
    Commented Jan 7, 2021 at 0:27
  • What do you expect to happen? You are defining an alias in one way and then redefining it afterwards. Rather than an alias, you're better off using a function and calling it something different. Commented Jan 7, 2021 at 1:01

2 Answers 2

2

You can use the aliases hash table for this:

% zmodload -F zsh/parameter p:aliases
% alias wget='wget --hsts-file="$XDG_CACHE_HOME/wget-hsts"'
% alias wget="$aliases[wget] -c"
% alias
wget='wget --hsts-file="$XDG_CACHE_HOME/wget-hsts" -c'

I'd probably also have to do some handling to fall back to the real command if the command wasn't already aliased before

That can be achieved by using ${aliases[wget]:-wget}. Then, if $aliases[wget] is empty, the literal value wget will be used instead.

-1

It can be done with some ugly parsing of the alias output:

$ alias foo='foo --first'
$ alias foo=$(alias foo | cut --delimiter== --fields=2 | head --bytes=-2 | tail --bytes=+2)' --second'
$ alias foo
alias foo='foo --first --second'

Of course, if alias ever prints the alias in any other format (multiple lines or different quotes) all bets are off. It also does not handle the case where the alias is not already set:

$ unalias foo
$ alias foo=$(alias foo | cut --delimiter== --fields=2 | head --bytes=-2 | tail --bytes=+2)' --second'
bash: alias: foo: not found
$ alias foo
alias foo=' --second'
2
  • OP uses Zsh, not Bash. Commented Jan 18, 2021 at 13:07
  • @MarlonRichert Not my fault IMO, since OP tagged it Bash originally. I'll leave this here for reference for any Bash users.
    – l0b0
    Commented Jan 18, 2021 at 18:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .