0

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.

1 Answer 1

0

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"'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a ton! I didn't even know you could make functions in config files like that. I mostly use aliases for static things that I'm tired of typing out (like alias zshow="defaults write com.apple.finder AppleShowAllFiles TRUE; killall Finder"). This is great though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.