Hacker News new | ask | show | jobs
by julian37 5592 days ago
Yet another time saver: if you need to execute only a single command in another directory, use:

  (cd /path; command)
This will cd to /path, run command, but return you to your original working directory. This works because the parens create a sub-process, and the cd command only affects that sub-process.
2 comments

I prefer (cd /path && command), since that way if the cd fails for some reason (like you've typo'd the path), it won't still attempt to run the command.
Of course! I actually do always use double-ampersand for chaining commands in bash, not sure what made me use a semicolon in that example. Thanks for the correction, a small but important distinction.
Appending ";cd -" on the end will have the same effect.