Reputation: 774
I need some help with a bash script that automatically add prefix in commit -m message ,it's not a server side ,just repo, i need to add message "User:..." , and if a user type commit message name for example "Jhon" , it will be User:Jhon . Maybe who can help to write a script for it ?
Upvotes: 1
Views: 552
Reputation: 5516
Here's what I would do.
Write a script (e.g. prefix_commit.sh
) that looks like this:
#!/bin/bash
git commit -m User:"$1"
Note:
git commit -m "User:$1"
will also work here.Make the script executable (you only need to do this once):
$ chmod +x ./prefix_commit.sh
Call the script from the command line and pass your commit message like so:
$ ./prefix_commit.sh 'Sample commit message'
Note:
Passing arguments to a bash script
Arguments can be received in a bash script or function like so:
$1 # first argument
$2 # second argument
$3 # third argument
### $4, $5, $6, $7 ... etc
So let's say I have a script that's called echo_my_args.sh
that echoes out three arguments:
#!/bin/bash
echo $1
echo $2
echo $3
I can pass three arguments to the script and see them echoed out:
$ ./echo_my_args.sh 'my first arg' 'my second arg' 'my third arg'
my first arg
my second arg
my third arg
Note here again that the argument must use single quotes if it has multiple words. There is no need for single quotes if you pass a single word argument:
$ ./echo_my_args.sh first second third
first
second
third
Here's some more information about how to pass arguments to a bash script.
Upvotes: 1