quotes in unix shell - shell

Trying to prepare simple shell commit script for git, like
git add .
git commit -m $#
git push origin master
to run it in ./script.sh 'commit message' manner
I ran into issue with commit messages containing space. I understand I need to wrap $# in quotes on string 2, but all my tries have failed so far(I tried straight ', then \', then fancy '\'' I found while researching, still no luck). Please help!

Just replace $# with "$#". That should do the trick.

If you're running the script as:
./script.sh 'commit message'
then the commit message is just $1, and your script should contain:
git commit -m "$1"

Related

Git alias for commit with branch name

I'm trying to create a simple bash alias to commit with my branch name in MacOs. For instance, if my branch if CS-12 I'd usually commit as follows:
git commit /file/location/myfile -m "CS-12 my message goes in here"
So I'm trying to create an alias which will receive only the file name and the message, ie:
gcm /file/location/myfile "my message goes in here"
I've got the following but it's not working:
alias gcm="echo git commit $1 -m \"$(current_branch) - $2\""
where current_branch is the function:
function current_branch() {
ref=$(git symbolic-ref HEAD 2> /dev/null) || \
ref=$(git rev-parse --short HEAD 2> /dev/null) || return
echo ${ref#refs/heads/}
}
which does work.
The output of running my alias:
gcm src/pages/register/Register.js "aasdasd asdasd"
is giving me back:
git commit -m master - src/pages/register/Register.js aasdasd asdasd
any idea what I'm doing wrong? Bash is not my area of expertise.
Thanks
The escaped quotes are 'stripped' by alias, so you need to escape them once more:
alias x="echo \\\"foo\\\""
x
"foo"
aliases do not take parameters. Just write a function:
gcm() { git commit "$1" -m "$(current_branch) - $2"; }
Note that there's really no need for aliases, and you shouldn't use them. Since at least 1996, the bash man page has stated: "For almost every purpose, aliases are superseded by shell functions."

git - how to get $(git_current_branch) variable from outside the git directory

I have a bash script containing the following code:
git -C "$1" push origin "$(git_current_branch)"
where "$1" is the git dir I am trying to push.
The problem is, I cannot get the correct "$(git_current_branch)" variable when I run the script from outside the git directory. I know I can just cd to "$1" before pushing, but I want a better solution (which makes my script cleaner).
Is there any way to do that? Thanks in advance!
Edit: I prefer to do this through git command option, if possible.
You can try that:
current_git_branch=$(git -C "$1" branch | sed '/^\*/!d;s/\* //')
explanation
call git in directory "$1" and print all branches
delete all branches without * at start of line
remove '* ' marker for current branch from line

How does eval stop the pathspec errors in this script?

I want to automate the many version control steps of Git. I was successful until I used git commit -S -m ${var} in my Bash script. This line gives me (pathspec errors x # of word) - 1... unless I use eval. How does eval make my script work?
I thought this article had the answer, but my issue involves a string, not an array.
Gif video of the broken vs. working Bash script
Broken code
brokenCommitCode () {
# Give it a multi-word, space-separated message
read -p 'Commit message (use quotes): ' commitMsg
commitMsg="'${commitMsg}'"
echo ${commitMsg}
git add -A &&
git commit -S -m ${commitMsg}
}
Working code
workingCommitCode () {
read -p 'Commit message (use quotes): ' commitMsg
commitMsg="'${commitMsg}'"
echo ${commitMsg}
git add -A &&
eval git commit -S -m ${commitMsg}
}
I expected the brokenCommitCode to commit properly with the message I enter on the prompt. The actual result is a pathspec error when it reaches git commit -S -m ${commitMsg}. How does eval make this work?
I'm using GNU bash, version 4.4.19(1)-release (x86_64-pc-msys) with git version 2.16.2.windows.1 on a Windows 8.1 PC.
Correct fix is
funname() {
read -p 'Commit message (use quotes): ' commitMsg
echo "${commitMsg}"
git add -A &&
git commit -S -m "${commitMsg}"
}
Why eval seems to fix:
single quotes where added to commitMsg variable (seems intent was to prevent message argument to be split on a whitespace)
looking what happens with the following message:
commitMsg="this is a message"
git commit -S -m ${commitMsg}
git commit -S -m this is a message
[error because "is" "a" "message" are taken as different additional arguments]
however it doesn't prevent because single quote is not re-interpreted but is like any other character in variable content
following with the example
git commit -S -m ${commitMsg}
git commit -S -m \'this is a message\'
[error "is" "a" "message'" are taken as different additional arguments]
with eval the single quotes are re-interpreted but also any other character which has a particular meaning in bash (;, &, ${..}, ..)
Suppose for example the following commit message which can inject arbitrary command.
commitMsg="message'; ls -l; echo 'done"
git commit -S -m 'message'; ls -l; echo 'done'

Using an other command in alias

When this command is run, it is not the time to submit the command, But print the source ~/.zshrc time.
~/.zshrc:
alias gitCommitAll="git add . && git commit -m \"`date +\"%T\"`\""
eg.
source ~/.zshrc at 10:00
gitCommitAll at 10:10 ==> git commit -m "10:00" not 10:10
Try this:
alias gitCommitAll='git add . && git commit -m "`date +%T`"'
Backquote (``) inside double quotes ("") will be executed prematurely. Try using single quotes ('') instead.
Don't use an alias; use a function instead. It makes quoting far easier. Something like
gitCommitAll () {
git add . && git commit -m "$(date +%T) $1"
}
How you handle the argument(s) to the function depend on what you intended the alias to do. It looks like you meant for all (or at least the first) "argument" to be part of the -m option, as a time alone isn't much of a commit message. The above just includes the first argument as part of the message.

bash how to make command work

I have following bash function in my ~/.bashrc
function gitlab {
MSG='first commit'
CMD="git commit -m '${MSG}'"
echo $CMD
$CMD
}
Here is the result
$ gitlab
git commit -m 'first commit'
error: pathspec 'commit'' did not match any file(s) known to git.
What's the fix?
BASH FAQ entry #50: "I'm trying to put a command in a variable, but the complex cases always fail!"
Definitely read BashFAQ/050 that Ignacio linked to.
You could try this, though:
function gitlab {
local PS4='Running: '
local msg='first commit'
bash -xc "git commit -m '$msg'"
}
I suppose you should use \" instead of ' so it should be something like:
CMD="git commit -m \"${MSG}\""
Try putting your commit message in double quotes, as single and double quotes mean different things to bash.
function gitlab {
MSG="first commit"
CMD=`git commit -m \"${MSG}\"`
echo $CMD
$CMD
}

Resources