2

I'm trying to alias a simpler stash so I can do git load <stashname>, something along this line:

load = !git stash list | grep ' $1$' | awk '{ print $1 }' | sed '$ s/:$//'; echo

Unfortunately, awk's $1 is being replaced by the stash name too. How can I escape that $ sign? Tried \$, but git says:

fatal: bad config file line 33 in /home/alvaro/.gitconfig
  • 2
    No idea about '$' escaping in git config files, but why not simply use an equivalent for that awk line, e.g. `sed 's/ .*//'` (snip everything after the first space). Or write this as a separate shell script instead of a git alias? – Andy Ross Oct 02 '12 at 23:02
  • @AndyRoss if you post that as an answer I'd be more than happy to accept it. – Álvaro Cuesta Oct 07 '12 at 12:18
  • What exactly are you trying to do in your oneliner? Just curious. – the.malkolm Dec 06 '12 at 19:04

1 Answers1

1

Neither occurrence of $1 is being replaced. They are surrounded by single quotes. The ; echo at the end of the line is just a comment. So it is not really doing anything. If you want that to be part of the alias you need to surround the whole thing with double quotes.

The <stashname> is being added to the end of the line (I assume that is what the echo is for?) The command you propose seems to want to echo something like stash@{n} given the name of the stash. The alias below will do that. (though it is not very useful)

[alias]
load = "!git stash list | grep \" $1$\" | awk '{ print $1 }' | sed '$ s/:$//' #"

The first occurrence of $1 is replaced by the argument because it is only surrounded by double quotes; which we need to escape. The $1 for awk will not be replaced be <stashname>. Besides git already allows you to apply, pop stashed by name.

$ git stash list # produces no output
$ echo "something" > else.txt
$ git stash something
Saved working directory and index state On master: something
HEAD is now at 6d3fcf0 merged
$ git load something
stash@{0}

perhaps it's easier to see it this way

[alias]
test = "! echo $1 '$2' \"$3\" end; # "

results in

$ git test first second third
first $2 third end
Honril Awmos
  • 192
  • 3