10

I've got a few aliases I want to clarify. They are working. How can I make this an option for many other aliases. Awk or grep? And pointers helpful. Thanks.

# IP addresses
alias myip="echo  '# myip  curl https://ipecho.net/plain; echo #'; curl -sS https://ipecho.net/plain; echo"
alias hazip="echo '# hazip curl https://ipv4.icanhazip.com     #'; curl -sS https://ipv4.icanhazip.com"
alias ips='myip && hazip'

 ~ ips
# myip  curl https://ipecho.net/plain; echo #
1.2.3.4
# hazip curl https://ipv4.icanhazip.com     #
1.2.3.4
2
  • 4
    Not sure what you're asking here: you can see the current alias definition using alias myip (without assigning anything to it). I guess if you want to see that everytime the alias is invoked you could define alias myip="alias myip; ..." Commented Apr 30, 2019 at 17:17
  • 1
    Use set -x before using alias and set +x after to restore. Better yet combine all three aliases into a function which does that for you. I'd write full answer but using phone. Commented Apr 30, 2019 at 17:28

3 Answers 3

13

Example ~/.bash_aliases file:

alias hi=" echo Hello"

You can modify this content to the following, to solve your problem:

alias hi=" type hi; echo Hello"

Output in case 1:

$ hi
Hello

Output in case 2:

$ hi
hi is aliased to `type hi; echo Hello'
Hello
1
  • Please mark as correct if you feel that this has solved your problem! Commented May 3, 2019 at 13:38
2

How about something like this:

shopt -s extdebug
shopt -u expand_aliases

function check_for_alias {
    COMMAND=$(alias $BASH_COMMAND 2> /dev/null)
    if [ $? -eq 0 ]
    then
        tput setaf 1
            echo $COMMAND
        tput sgr0        
        ${BASH_ALIASES[$BASH_COMMAND]}
        return 1
    fi
}

trap check_for_alias DEBUG

Explanation:

I use so called bash DEBUG trap to call a function before any command. Inside I can use $BASH_COMMAND variable to view what is currently being called:

function check_for_alias {
    echo $BASH_COMMAND
}
trap check_for_alias DEBUG

This won't do for aliases because in the handler they are already expanded - I have to disable their expanding, and then call them manually:

shopt -u expand_aliases

function check_for_alias {
    # ...
    ${BASH_ALIASES[$BASH_COMMAND]}
}

Unfortunately, bash still tries to run an unexpanded alias, so I reveive an error telling me that command is not found. To bypass this I enable debugging and return 1 from handler if I detect that the command I run is an alias

shopt -s extdebug

function check_for_alias {
    COMMAND=$(alias $BASH_COMMAND 2> /dev/null)
    if [ $? -eq 0 ]
    then
        # ...
        return 1
    fi
}

Finally to print alias I use tput to use red font. Output command is stored in $COMMAND variable.

tput setaf 1
    echo $COMMAND
tput sgr0  
3
  • Thank you for providing this solution. Normally my upvotes equal about i. Makes me feel better to see a real response. Have to go with the one at about my pay grade. Commented May 6, 2019 at 13:00
  • I tried putting the first block of code in my .bash_aliases to run. It didnt work. I tried adding the last code block, but it didnt work. Am i missing something?
    – alchemy
    Commented Mar 26, 2022 at 3:40
  • Interesting approach, but a tad over-engineered, in my opinion: too many hard to understand things like command switches, traps and error output redirection. The latter is dangerous, cause there's a risk to miss a real error on command failing. The same problem applies to traps... I wrote more straightforward solution below, which is easier to understand, hence, to support/expand etc. I've been using it on daily basis and it works well for me... Commented Feb 11, 2023 at 12:57
2

First of all, I need to stress on the fact, that there is no ready solution to echo alias command before execution. At least, I couldn't find anything like that... But it doesn't mean, that we can't leverage bash flexibility to come up with whatever we want!

The solution below consists of a couple of parts, but there is nothing hard to implement on any Ubuntu system:

  1. Create a dedicated file in your home folder or any other folder, suitable for you. Let it be ~/.aliases

  2. The contents of this file must be something like this:

als() {
  alias_name="$1"
  cmd="$2"
  alias $alias_name="echo ; echo $cmd ; echo && $cmd"
}

als gl "git log"
als glns "git log --name-status"
als sshc-router "ssh [email protected]"

To understand, how it works, let's take a look at the first command: als gl "git log" This is a regular call for bash function, that accepts two parameters: gl is alias name and "git log" is alias command.

The contents of als() function are quite straitforward: it just builds echoed output for each passed command. For the command above, the final alias will look like the following:

alias gl="echo ; echo git log ; echo && git log"

(Empty echo's required to add more new lines for the command to see it better.)

As you can see, the way of adding new alias commands is even simpler, than original command structure alias name=command.

  1. After you created a file and added all the needed aliases to it, all you have to do is to add the following to your ~/.bashrc file:
ALIAS_FILE=$HOME/.aliases
if [ -f "$ALIAS_FILE" ]; then
    source "$ALIAS_FILE"
fi

This ~/.bashrc file is called every time on terminal opening. Consequently, this aliases will be available in your console without any additional activities from your side! Nice and neat!

  1. And the last step. Let's just open the console and test out our solution:
user@user-pc:~/work/projects/your-project$ gl

git log

commit 543c1d070c0b52a510def7ec8a451472c513019c (HEAD -> feature/FEATURE-999-branch)
Author: John Doe 
Date:   Tue Jan 24 18:01:14 2023 +0100
1
  • 1
    Good idea! I'd still prefer type instead of echoing cmd. Also, instead of cmd, I'd have preferred alias_action or alias_cmd to understand the variable better. Either way, you have my (first) upvote! Commented Feb 11, 2023 at 13:54

You must log in to answer this question.

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