6

I've made several aliases for my convenience.But now I need to be helpful by sending a useful command and it is full of aliases.I've tried doing type u but all it returns is

up && ap upgrade -y --show-progress && r && ap check && ap autoclean

These are all aliases in u:

alias a='alias'
a ap='apt-get'
a r='ap autoremove -y'
a up='ap update'
  • I apologize, I am having trouble understanding your question. When you say type u and "These are all aliases in u", what is u? And is up && ap upgrade -y etc...... the command you are trying to execute? If yes, then can you tell us in what way it doesn't work? – Celada Jul 26 '14 at 10:36
  • Take a look at help unalias. – Cyrus Jul 26 '14 at 10:38
  • @Celada No need to apologize, I could have explained it more.type command says something about something so type u says something about alias named u The problem is it returns what the command u does but it contains more aliases. – user251046 Jul 26 '14 at 10:40
  • @Cyrus is this pernament? – user251046 Jul 26 '14 at 10:41
  • @user251046: no. – Cyrus Jul 26 '14 at 11:32

2 Answers2

15

Press Ctrl-Alt-e with a command using your aliases written (ready to run) and Bash will expand it. Ctrl-Alt-e is the default binding for the shell-expand-line readline command.

Each time you push Ctrl-Alt-e Bash will expand one layer of alias, so push it repeatedly until your command is expanded as far as you need.

If your Meta key is not Alt, substitute it instead, or press Escape Ctrl-e.

There is also an alias-expand-line function which is not bound by default, which only expands aliases.

Michael Homer
  • 76,565
  • You'll need to keep pressing this key combination until the line is fully expanded if you have aliases within aliases as is the case with the OP. – Joseph R. Jul 26 '14 at 10:47
  • @JosephR. Oh thanks, I've found it out myself anyway though. – user251046 Jul 26 '14 at 11:25
  • @user251046 I figured you would. This is just a caveat for any future readers. – Joseph R. Jul 26 '14 at 11:49
  • 2
    @JosephR. to be taken with a grain of salt for recursive aliases (like alias ls='ls --colour') – Stéphane Chazelas Jul 26 '14 at 11:51
  • @StéphaneChazelas haha it actually makes text art. – user251046 Jul 26 '14 at 11:58
  • 1
    Ctrl-Alt-e doesn't expand only aliases. It also expands variables, arithmetic and command substitutions (and process substitutions!), though not tilde or globs, all of them on the command line, so use with care! alias-expand-line would be a better widget but is not bound by default. See also Ctrl-X a in zsh to expand the alias under the cursor. – Stéphane Chazelas Jul 26 '14 at 11:59
1

I posted a similar question on StackOverflow (probably should have posted here on Unix & Linux).

My solution was to write a bash function:

xtrace() {
    local eval_cmd
    printf -v eval_cmd '%q' "${@}"
    { set -x
      eval "${eval_cmd}"
    } 2>&1 | grep '^++'
    return "${PIPESTATUS[0]}"
}

So xtrace u will print out something like:

++ apt-get update
++ apt-get upgrade -y --show-progress
++ apt-get autoremove -y
++ apt-get check
++ apt-get autoclean

Keep in mind that this actually executes whatever alias you pass to it, so if you don't want to execute u, then @MichaelHomer's solution is the better way to go. Also, because of your && chain, if any one of the commands in your alias does not return 0, then execution will stop and you will only see the expanded aliases up to that point.

Mattman
  • 11
  • 3