2

I try to write an alias to check the git status for all packages under the same workspace. It is showed below:

alias ws-git-status="for d in *; do (cd $d && pwd && git status);done"

When I run the alais under my workspace, it reports error:

4702 ◯  pwd
/Users/adam/workplace
4c327596e4c7 ॐ  ~/workplace:
4703 ◯  ws-git-status
/usr/share/zsh/5.2/functions
fatal: Not a git repository (or any of the parent directories): .git

But if I use single quote in the alias definition:

alias ws-git-status='for d in *; do (cd $d && pwd && git status);done'

It works pretty well. I search the diff between double and single quotes Difference between single and double quotes in Bash

It seems the double quote will take the value of $ but single quote will treat it like a character, which can not explain my case. Could anyone help me with some explanation or useful links? I am confused about this matter and unable to figure it out.

dashenswen
  • 540
  • 2
  • 4
  • 21

2 Answers2

4

Variable references like $d are expanded inside double quotes. So in the case of the alias definition you quote, $d is expanded when you define the alias which is not what you want. Type alias ws-git-status to see how it has been defined.

If you use single quotes, $d does not get expanded when defining the alias and it will work as you would expect.

okapi
  • 1,340
  • 9
  • 17
1

You should use an alias for this at all; define a function:

ws-git-status () {
    for d in *; do
      (cd "$d" && pwd && git status)
    done
}
chepner
  • 497,756
  • 71
  • 530
  • 681