1

I have this function

snip-git(){
    cd  ${snippetdir}
    git add .
    git commit -m "."
    git push -u
}

When git finishes pushing the changes, I want to get back to the directory I was when I called snip-git.

I tried this

snip-git(){
    cwd=$(pwd)
    cd  ${snippetdir}
    ...
    cd cwd
}

But it ends up reading the ned directory rather than the old one

relidon
  • 115
  • 4

1 Answers1

3

The easiest way is making your works in a subshell only:

snip-git() (
    cd  -- "$snippetdir"
    git add .
    git commit -m "."
    git push -u
)

There're some thing you want to fix in your function:

cuonglm
  • 150,973
  • 38
  • 327
  • 406
  • That's not working! I'm on root directory (but I could be anywhere), I run `snip-git' at the end of push, I'm not redirected to the root – relidon Mar 22 '16 at 10:28
  • @relidon: Did you change the `snip-git` definition to use `(...)` instead of `{ ... }`? – cuonglm Mar 22 '16 at 10:29
  • Now I did :) and it works. What's the difference in `( )` and `{ }`. Thanks @cuonglm – relidon Mar 22 '16 at 10:35
  • @relidon: As I said in my answer, `( )` ran your commands in subshell, after your command done, the environment was restored. – cuonglm Mar 22 '16 at 10:36