Define Alias That References Other Aliases

Define alias that references other aliases

To reuse alias in another alias use:

foobar='foo;bar'

However I would suggest you to consider using shell function to get better control over this.

Cmder : how to use an alias in another alias?

Benj:
I suspect that you've already found an answer by now, but I see that no one has answered this question, so I will.

If I may make an alternative suggestion to the aliases file. It looks like you're trying to solve this for git.

The best way I've found to do this in Windows is to edit the ~/.bashrc file. On most Windows machines it would be under C:\Users\<USERNAME>\.bashrc.

You can create a function such as:

function MergeLocDistFn {
local co_branch=$1
local merge_branch=$2

echo ">> Debug Command: git checkout ${co_branch}"
git checkout ${co_branch}

echo ">> Debug Command: git merge --ff-only --verbose origin/${merge_branch} || git rebase --preserve-merges --verbose origin/${merge_branch}"
git merge --ff-only --verbose origin/${merge_branch} || git rebase --preserve-merges --verbose origin/${merge_branch}
}

Then you can create aliases in the ~/.bashrc file, like the following:

alias mergelocdist=MergeLocDistFn
alias mm='MergeLocDistFn master master'

The advantage to this approach is that it will allow you to get really sophisticated with the function and as I'm showing here, debug it as well with echo statements.

See the attached picture of the example output.
example output from bashrc

Note also, if you'd rather do this for a PowerShell prompt, it's even easier to modify the PowerShell profile, just let me know and I'll show you how.

Another note, you can also create a function in the .bashrc for the current branch. Here's an example function:

function current_branch() {
ref=$(git symbolic-ref HEAD 2> /dev/null) || \
ref=$(git rev-parse --short HEAD 2> /dev/null) || return
echo ${ref#refs/heads/}
}

Here's an example of using the current_branch function:

alias gpush='git push -u origin $(current_branch)'

I hope that his helps, just let me know if you need any clarifications on this.

BASH - alias calling another alias OR alias calling itself OR a recursive alias

The dstuff alias ends with ;, so when dsc is expanded you get echo;; dkm at the end. Shell syntax doesn't allow two ; in a row.

Get rid of the ; at the end of the dstuff alias.

Reference variable in alias

Since you define your alias using double quotes, the $N is expanded before definition and your alias actually is echo 4.

Try this :

$N=4
$alias sayn='echo $N'
$N=2
$sayn
2

Make a Bash alias that takes a parameter?

Bash alias does not directly accept parameters. You will have to create a function.

alias does not accept parameters but a function can be called just like an alias. For example:

myfunction() {
#do things with parameters like $1 such as
mv "$1" "$1.bak"
cp "$2" "$1"
}

myfunction old.conf new.conf #calls `myfunction`

By the way, Bash functions defined in your .bashrc and other files are available as commands within your shell. So for instance you can call the earlier function like this

$ myfunction original.conf my.conf

In Git is it possible to refer an alias from another alias

In versions of Git before 2.20:

Not that way, but you can make an alias run a command through the shell, hence running another instance of git which resolves the second alias:

alias.sl = !git showlog --abbrev-commit

In 2.20 or later, see VonC's answer.



Related Topics



Leave a reply



Submit