I currently have this alias:

alias cmakerel='cmake -DCMAKE_BUILD_TYPE=Release -Wno-dev ../../ && make -j4'

Essentially, it will call cmake to do an out-of-source build from PWD. It works fine, but as-is, it can be run from nearly anywhere (which I don't want).

How do I modify this alias so that it runs if and only if the string bld is in my present working directory?

I need this change because without it, I sometimes accidentally invoke this command from the tst directory or some other directory where I just cause a total mess.


From man bash:

For almost every purpose, aliases are superseded by shell functions.

So make it a shell function.

function cmakerel {
    if expr match "$PWD" '.*bld.*' >/dev/null ; then
        cmake -D....
        echo "Wrong directory!"

It's a regular expression you can adjust to your needs.

  • Great tip! I would give you +5 if I could. TYVM. – kfmfe04 Dec 16 '11 at 21:51
  • btw, how should I modify cmakerel above to return some failure value so when I call cmakerel && make check the second part doesn't get called on failure? – kfmfe04 Dec 25 '11 at 20:12
  • 2
    @kfmfe04 Add a line exit $? below the cmake call. It will then exit with the exit code of the last command performed. Unless you use pipes, it's straightforward. Below the echo call, add exit 1. – Daniel Beck Dec 25 '11 at 20:29
  • great - thx! Add this as an answer to the Question I just posted and so I can send you some points. – kfmfe04 Dec 25 '11 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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