Reputation: 16383
I have a binary (emulator
from the Android SDK Tools
) that I use frequently. I've added it to my $PATH
variable so that I can easily run the command with solely emulator
.
However, it turns out that the binary uses relative paths; it complains if I run the script from a different directory. I'd really like to run the command from any folder without having to cd
into the directory where the binary lives. Is there any way to get the script/bash into thinking I ran the command from the directory that it's located in?
Upvotes: 1
Views: 82
Reputation: 295815
A function is an appropriate tool for the job:
emu() ( cd /dir/with/emulator && exec ./emulator "$@" )
Let's break this down into pieces:
cd
only takes effect for that subshell, and doesn't impact the parent.&&
to connect the cd
and the following command ensures that we abort correctly if the cd
fails.exec
tells the subshell to replace itself in memory with the emulator, rather than having an extra shell sitting around that does nothing but wait for the emulator to exit."$@"
expands to the list of arguments passed to the function.Upvotes: 2
Reputation: 88949
Add an alias (emu) to your ~/.bashrc:
alias emu="(cd /dir/with/emulator; ./emulator)"
Upvotes: 1