Frage

I'm using rerun and sinatra enough that I wanted a nice alias so I could execute rerun ruby X.rb where X was the current directory (ie: /Users/David/Documents/Projects/sample=>rerun ruby sample.rb).

The command I'm using (adapted from this question) is alias sin="rerun ruby ${PWD##*/}.rb". When I use that, it says ruby: No such file or directory -- David.rb (LoadError) as if it was passed my user folder instead of the project folder.

However, when I manually source my .zshrc (alias sz="source ~/.zshrc", which should be run on shell creation (and all my other aliases work fine, so I have no reason to believe it wouldn't be working)) and run sin again, it totally works as intended.

Any ideas? I'm pretty new at bash scripting. Also, I'm using zsh if that's important.

War es hilfreich?

Lösung

The general rule about aliases is "if you have to ask, use a function instead":

sin() {
  rerun ruby "${PWD##*/}.rb"
}

which works as expected.

The particular problem in your case is that ${PWD##*/} is expanded when you define the alias, and not when you run it. You could also fix it by using single quotes in the definition:

alias sin='rerun ruby "${PWD##*/}.rb"'
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top