I want to easily set an alias git-go
to this terminal line:
git commit -m "init "; git push; git status
So when I enter git-go this line should enter.
How can I do that? The answers I seen only cover alias of a command without parameters. But I want to set an alias to an arbitrary terminal line.
Edit: I've learned to use functions instead of aliases because
The rules concerning the definition and use of aliases are somewhat confusing.
and
For almost every purpose, shell functions are preferred over aliases.
So don't use an alias unless you have to. https://ss64.com/bash/alias.html
You do this the same way you would set any alias.
The situation where it gets tricky is not when an alias runs a command and passes arguments to that command, nor even when an alias runs multiple commands separated by
;
, but instead is when you want an alias to accept and use its own command-line arguments.For example, anything you write after the name of that alias will be pasted onto the end, and thus passed as command-line arguments to the third
git
command, aftergit status
. (Really it's not so much that the following text is pasted onto the end, as much as it is that the following text is left alone and the alias name is replaced with its definition.)So you can run your alias without arguments, which works, and the last command is
git status
:Or you can run it with arguments that you want passed to
git status
. For example, when you run it this way, the last command isgit-status --show-stash
:What you cannot do with an alias in Bash (and other Bourne-style shells) is to make the alias accept command-line arguments and place them elsewhere than the end.
For example, suppose you wanted
git-go
to accept an argument that it uses for the commit message. You would not be able to write this as an alias. The solution would be to write it as a shell function instead:In the definition of a shell function, the positional parameters
$1
,$2
, and so forth hold the values of the command-line arguments passed to the shell function. Aliases have no functionality that corresponds to this, because alias expansion is really a form of macro processing, taking place very early, when the shell parses a command.You can, of course, write it as a shell function even if you don't need to use positional parameters in the definition, as Videonauth suggests.
You can declare it a function in your
~/.bash_aliases
file like so:or you can create an alias in the same file like so:
Do not forget to reopen your terminal or source the file (
. ~/.bash_aliases
) after you changed it.As you are dealing with
git
commands, you might also want to know the syntax for adding an alias especially forgit
:This will add a new alias to your local
git
configuration (at~/.gitconfig
) and allow you to issuewhen inside a
git
repository. Whenever you rungit go
a shell is invoked and the commandgit commit -m "init"; git push; git status
is passed to it.See the git documentaion for further details.
It was a quoting problem. I had tried this:
But got this error:
I got that error because I was using nested quotes incorrectly. Here's the correct syntax:
Or:
Or without quotes if no white space in the commit message: