1

On a CentOS with Bash with MariaDB and PHP 8.1 I use Drush.
Drush is the command line for Drupal, a content management system.

Each time I run a Drush command I write like:

cd /home/www/example_1.com
vendor/bin/drush status
vendor/bin/drush cache:rebuild

or

cd /home/www/example_2.com
vendor/bin/drush status
vendor/bin/drush cache:rebuild

How could I utilize PATH to no longer need vendor/bin for Drush commands?

export PATH=$PATH:vendor/bin

Is this the way to go?

tainu
  • 11
  • 2

1 Answers1

0

A workaround (and maybe there are better ways) is about updating the $PATH every time you get the shell prompt and that's possible by using PROMPT_COMMAND environment variable (in bash).

So you can define a function like this:


#You can modify this function to update the path
#only when the current directory is under '/home/www/website_folder'

checkPath() { 

if [[ -z $OLDPATH ]]; then  
  OLDPATH="$PATH";  
  PATH="${PATH}:${PWD}/vendor/bin"; 
else 
  PATH="${OLDPATH}:${PWD}/vendor/bin"
fi 

}

The if condition prevents adding a new path every time you run a command. So you will have always your original $PATH + $PWD/vendor/bin where $PWD is the working directory

And in your shell set this:

PROMPT_COMMAND='checkPath'

With the code above every time you get the shell prompt (when a command finished its execution the PROMPT_COMMAND content (checkPath function in this case) is executed).

So if you type:

cd /home/www/example_1.com

The $PATH will have:

/home/www/example_1.com/vendor/bin

When you change to example_2.com like this:

cd /home/www/example_2.com 
#or 
cd ../example_2.com

The $PATH will have:

/home/www/example_2.com/vendor/bin

Disadvantage

Using this workaround might be useful but if you change to a directory such as /home/www the $PATH will have this value:

/home/www/vendor/bin

As you can see, the $PATH might have a non-existent directory but AFAIK this will not have issues with your shell, remember that the PATH is changed every time (when you cd to another directory). In case of /home/www/vendor/bin exists then the files under that directory will be recognized by the PATH variable.


Also, if you had drush installed globally in your system (e.g. in /usr/bin) then the command drush provided by /home/www/example_?.com/vendor/bin/ will not work.

A solution to this is to prepend ${PWD}/vendor/bin to $PATH instead of appending it:

checkPath() { 

if [[ -z $OLDPATH ]]; then  
  OLDPATH="$PATH";  
  PATH="${PWD}/vendor/bin:${PATH}"; 
else 
  PATH="${PWD}/vendor/bin:${OLDPATH}"
fi 

}
Edgar Magallon
  • 4,711
  • 2
  • 12
  • 27