I have a directory like this:


Inside Project and after many sub directories, I have a script names ninja that needs to be run with ./ninja. In other words:


Of course, I can just cd into Project and execute ./ninja. However, I am writing an alias to run that command via bashrc.

alias runNinja = cd ~/Project/sub1/sub2/sub3 && ./ninja

Can we do it 1 command?

alias runNinja = .~/Project/sub1/sub2/sub3/ninja

The above obviously doesnt work, or even this

alias runNinja = ./home/user/Project/sub1/sub2/sub3/ninja

TL,DR: How to shorten alias to run script in directories?

  • if the script needs to be run in the right working directory this will be hard to achieve with an alias. You should consider a shell function or a wrapper script instead. stackoverflow.com/questions/786376 – cg909 Apr 16 '20 at 21:29
  • You say shorten. Do you have a working but longer version? – ctrl-alt-delor Apr 16 '20 at 21:41

Just append the path in the $PATH Variable in .bashrc like below:

export PATH=$PATH:/home/user/Project/sub1/sub2/sub3

And execute it from wherever you want in even without ./

$ ninja

But ofcourse you can set alias also

alias runNinja='/home/user/Project/sub1/sub2/sub3/ninja'

And execute from wherever:

$ runNinja

If you deliberately wanted to be in that directory when running this ( Ex:if you are processing any file as input/output from that directory or dependency) , you have to write a function like below in your ~/.bashrc file or profile:

runNinja() { cd /home/user/Project/sub1/sub2/sub3 && ./ninja "$@" }

Aliases in bash are written

alias ninja="$HOME/Project/sub1/sub2/sub3/ninja"

(no spaces around the =).

I've also used the variable $HOME here, which will be expanded to the path of your home directory when the alias is defined.

Using a shell function instead:

ninja () {
    "$HOME/Project/sub1/sub2/sub3/ninja" "$@"

The "$@" will be expanded to any arguments that you may give on the command line.

If you need to change the current directory to $HOME/Project/sub1/sub2/sub3 before running ./ninja, you could do that in a function as well:

ninja () (
    cd "$HOME/Project/sub1/sub2/sub3" &&
    ./ninja "$@"

Note that I put the function body in a subshell ((...)). This means that the cd will not change the current directory of the current shell, but only in the environment in the function.

You could obviously have written that as

ninja () {
    ( cd "$HOME/Project/sub1/sub2/sub3" && ./ninja "$@" )

The && between cd and the ninja command ensures that ./ninja will not be executed if the cd fails.

Shell functions may be declared wherever you would usually add aliases (possibly in ~/.bashrc).


You are so close. However the . is not a command, it is part of the file name.

The file . is in every directory, is is a directory it is the same directory as the one that it is in. cd ././././././ will get you nowhere. $PWD, ., ./., and ././. are all the same directory.

Therefore to run /home/user/Project/sub1/sub2/sub3/ninja you type /home/user/Project/sub1/sub2/sub3/ninja To make an alias you do
alias ninja="/home/user/Project/sub1/sub2/sub3/ninja"

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.