Skip to content
Advertisement

Executing a script from a parent directory?

I have a root directory.

In there I have ./bin/

In ./bin/ I have some bash scripts like:

launch_server.sh port=3000

These scripts must be ran from ./bin, that is, the working directory must be:

home/blah/whereever/root/bin

however, I would like to do:

cd home/blah/whereever/root/
start launch_server.sh port=3000

That last line would set the working directory temporarily to ./bin/ and would execute my script found in ./bin and pass that script any arguments such as port=3000

Is this possible? What is the most convenient way I can launch my bin scripts from the root directory?

The reason is the paths need to stay relative, so I can’t hardcode anything

Advertisement

Answer

1st solution:

In your ~/.bashrc:

alias launch_server="home/blah/whereever/root/bin/launch_server.sh"

Apply the changes:

source ~/.bashrc

In your launch_server.sh script, add before any other command:

cd home/blah/whereever/root/bin/

Now, simply call from anywhere:

launch_server port=3000

2nd solution:

The start script you want should probably look like:

#!/bin/bash
cd ./bin
eval "$@"

Now call:

  • bash start launch_server.sh port=3000 or
  • ./start launch_server.sh port=3000
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement