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:
Create a dedicated file in your home folder or any other folder, suitable for you. Let it be ~/.aliases
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
.
- 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!
- 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
alias myip
(without assigning anything to it). I guess if you want to see that everytime the alias is invoked you could definealias myip="alias myip; ..."
set -x
before using alias andset +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.