0

i am writing a quick gitpush.sh file.

git add -u
git commit -m 'quick update: '$1
git push origin master
echo $1

when run it i want to add a custom message to it. so instead of writing:

sh gitpush.sh a_commit_for_lala_land

which is considered $1

i want to write

sh gitpush.sh a commit for lala land

without the underscores. how to i modify $1 to sum up multiple arguments with a space in between each argument to form a sentence?

EDIT I know this might not be proper git usage. but i only need it for an insignificant project i am working on alone (no branches etc).

2
  • What is so terrible about using quotes? sh gitpush.sh "a commit for lala land"? Commented May 12, 2018 at 16:10
  • Your script should be quoting $1 in both cases anyway. Commented May 12, 2018 at 16:10

2 Answers 2

2

Use quotes properly:

git add -u
git commit -m "quick update: $1"
git push origin master
echo "$1"

Then when you call the script:

sh gitpush.sh "a commit for lala land"

If you really object to using quotes, have the script prompt you for the message instead of passing it as multiple arguments:

printf 'Commit message: ' >&2
IFS= read -r msg
git add -u
git commit -m "quick update: $msg"
git push origin master
echo "$msg"
Sign up to request clarification or add additional context in comments.

6 Comments

@zwer answer made sure i don't need to add any quotes (i am not that lazy but it covers my current need). Thank you for the response though.
Just don't include any asterisks in your commit message. sh gitpush.sh add an * to a comment isn't going to do what you want. Really, it's better to do things the right way than to assume you are never going to get bitten by doing it the wrong way.
@GeorgePamfilis Without quoting, you wouldn't be able to say, e.g., I'm just committing a day's worth of work.
@Kusalananda i just tried your message and it stripped away the '. it showed up as ` 0922bef..64b7011 master -> master 'Im just committing a days worth of work'` It's fine. ill keep it in mind
@GeorgePamfilis That's his point. You would have to escape the single quotes somehow if you wanted them as part of the commit message, or else the shell removes them before the string is ever passed to your script.
|
2

You can concatenate all the arguments using $*, e.g.:

git commit -m "$*"

NOTE: It will use the value of $IFS as a separator while concatenating - if it's defined as something other than a space you can temporary re-define it before calling $*.

2 Comments

The single quotes don't serve any purpose here, other than adding them to the commit message itself.
@chepner - True, I forgot to remove them from the OPs original line. Cleaned up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.