Alias with Parameters

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

ZSH alias with parameter

You can't make an alias with arguments*, it has to be a function. Your function is close, you just need to quote certain arguments instead of the entire commands, and add spaces inside the [].

gitall() {
git add .
if [ "$1" != "" ] # or better, if [ -n "$1" ]
then
git commit -m "$1"
else
git commit -m update
fi
git push
}

*: Most shells don't allow arguments in aliases, I believe csh and derivatives do, but you shouldn't be using them anyway.

How to pass command line arguments to a shell alias?

You found the way: create a function instead of an alias. The C shell has a mechanism for doing arguments to aliases, but bash and the Korn shell don't, because the function mechanism is more flexible and offers the same capability.

How can I use an Alias as function parameter, In PowerShell?

An Alias, in simple terms, is just an association to a cmdlet, function, script file, or executable program. When we Set-Alias, what's happening is that a new AliasInfo instance is added to the Alias: PSDrive - See about Alias Provider for details.

So, to recap, when we do:

Set-Alias -Name github -Value "C:\UserName\Folder\github"

What's actually happening is that the the alias with name github is being associated with the function with name C:\UserName\Folder\github, a fun but useless demonstration:

PS /> Get-Alias github

CommandType Name Version Source
----------- ---- ------- ------
Alias github -> C:\UserName\Folder\github

PS /> ${function:C:\UserName\Folder\github} = { 'hello' }
PS /> C:\UserName\Folder\github
hello

PS /> github
hello

In my opinion, an easy workaround for what you're looking for would be to have one function (github) that outputs a string containing the path you want to access and a second function (goto) that receives the pipeline input, for this we use a non-advanced function that leverages the automatic variable $input:

function goto { Set-Location -Path $input }
function github { 'path\to\something' }

github | goto

Set-Alias would also be a valid use case for Set-Location since this cmdlet already accepts values from pipeline:

Set-Alias -Name goto -Value Set-Location
function github { 'path\to\something' }

github | goto

Alternatively, JPBlanc suggested, in a now deleted answer:

function goto ([string] $alias) { Set-Location -Path $alias }   
function githubpath { "C:\UserName\Folder\github" }

Which would also work for a positional argument but mandates the use of the grouping operator (..):

goto (github)

Why ?

Without the grouping operator, the function goto would see github as an argument and NOT as a separated function which we want to invoke.

The operator forces the github function to be invoked first or, in other words, to take precedence and to produce it's output before the invocation of the goto function.

Shell - Remove space before variable in alias

If you use double quotes, the variable is expanded right when parsing the alias definition.

Aliases don't take parameters, they just append the remaining words - you can't "remove the space", it has already been parsed.

Use a function instead, functions take arguments:

myssh () {
ssh user@127.0.0."$1"
}

Passing argument to alias in bash

An alias will expand to the string it represents. Anything after the alias will appear after its expansion without needing to be or able to be passed as explicit arguments (e.g. $1).

$ alias foo='/path/to/bar'
$ foo some args

will get expanded to

$ /path/to/bar some args

If you want to use explicit arguments, you'll need to use a function

$ foo () { /path/to/bar "$@" fixed args; }
$ foo abc 123

will be executed as if you had done

$ /path/to/bar abc 123 fixed args

To undefine an alias:

unalias foo

To undefine a function:

unset -f foo

To see the type and definition (for each defined alias, keyword, function, builtin or executable file):

type -a foo

Or type only (for the highest precedence occurrence):

type -t foo

Set up git alias with an input argument

The trick to more complex aliases, including ones that concatenate parameters to other text, is to create and immediately run a shell function in the alias command:

git config --global alias.rmalias '!f() { git config --global --unset "alias.$1"; }; f'

This creates a function f which uses the first parameter $1 in the Git command it runs. Then it runs the function f (which passes all the parameters given to the alias on the command line as parameters to the function).

Creating these kind of aliases on the command line requires extreme care with the spacing and quoting, so copying and pasting is a smart idea -- if you use double quotes around the whole alias your shell may do history expansion on ! and break the alias, and you leave out the spaces around the punctuation in the function definition, that can make the function definition invalid.

Creating a git alias with a parameter

You can instead use a shell function:

pomt = "!f(){ git push origin master && git tag \"$1\"  && git push --tag; };f"

Note: I would recommend created an annotated tag instead of a lightweight one.


Alternative:

Make a script called git-pomt anywhere on your %PATH% (no extension): it will be regular bash script (again, works even on Windows, as it is interpreted by the git bash)

In that script, you can define any sequence of command you want, and you would still call it with git pomt mytag (no '-': git pomt)

#!/bin/bash
git push origin master
git tag $1
git push --tag


Related Topics



Leave a reply



Submit