Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Input to Bash Alias?

Tags:

git

alias

bash

Whenever I'm using git, I generally add, commit, and push at the same time. So I created an alias in bash to make it simpler:

alias acp='git add -A;git commit -m "made changes";git push'

How do I make it so that the I can change the commit message from "made changes" to something else while I'm running the acp command? For example:

acp "added the Timer Class" 

The above I would want to run everything that the acp command does and make "added the timer class" the commit message. How would I do that?

Thanks!

like image 499
hmir Avatar asked Oct 14 '13 11:10

hmir


People also ask

Can a bash alias take arguments?

Bash users need to understand that alias cannot take arguments and parameters. But we can use functions to take arguments and parameters while using alias commands.

How do you pass arguments to alias commands?

You can replace $@ with $1 if you only want the first argument. This creates a temporary function f , which is passed the arguments. Alias arguments are only passed at the end. Note that f is called at the very end of the alias.

How do you call an alias in bash?

Open ~/. bashrc file in any editor, add alias command in that file, save the file and run the `source` command to re-execute the file with added alias command.

Where do I put alias in bash?

Keeping bash alias in a different file Now you can add any aliases in your ~/. bash_aliases file and then load them into your Bash session with the source ~/. bashrc command.


1 Answers

An alias cannot accept parameters, so you need to create a function:

acp ()
{
        git add -A;git commit -m "$1";git push
}

as always, store it in ~/.bashrc and source it with source ~/.bashrc.

Or better (good hint, binfalse) to avoid performing a command if the previous was not successful, add && in between them:

acp ()
{
        git add -A && git commit -m "$1" && git push
}

Execute it with

acp "your comment"

It is important that you use double quotes, otherwise it will just get the 1st parameter.

like image 56
fedorqui 'SO stop harming' Avatar answered Nov 11 '22 07:11

fedorqui 'SO stop harming'