Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I perform a recursive search to find a command I executed few days back and I want to run it. However that command should run from the path from where I ran it previously from and not where I currently am.

Is it possible for zshto figure this out and change the command to the full path and run from my current path? or maybe fork a process switch to the original path run it and come back?

share|improve this question
If zsh could record the PWD for every path, another use-case for it would be to filter the history search based on current working directory. – HRJ Nov 6 '14 at 11:08
Just found out that Oh-my-zsh has a plugin that records per-directory history! – HRJ Nov 6 '14 at 11:13

The original directory where the command was executed is not recorded in the history.

You can set a hook to be executed when the name of the command you try to execute is not found: command_not_found_handler. But unless you have a good idea of what previous working at that time must have been, this isn't going to get you far.

Related reading: zsh history - full path; Can I have my shell history record how wildcards expanded?

share|improve this answer

In most cases, this would actually be an undesirable action. Often when I rerun command out is specifically to run them in my current context. Running a history command and having it act on something other than the current folder could even have unexpected and unwanted side effects.

I would suggest pursuing a different approach. First identify what command need to be specifically tin from a certain location, then ask yourself why they are like that and fix them using standard patterns.

  • If they need to load libraries out resources that are at paths relative to the executable, fix them so that they know where to find these using things like dirname and $0.

  • If they need data, consider passing them arguments that show where to find the data that are not relative so they can be so reused.

  • If the source of the issue isn't fixable by you, consider writing small shell functions or wrapper scripts that setup the environment for the executable independently of whatever factors vary on your system.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.