[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash: testing a variable

Am 09.07.21 um 21:54 schrieb Charles Curley:
I'd like to check two things:

* Whether a given path is not already in the $PATH variable

* Whether the given path already exists

If both are true, I'd like to add the given path to the $PATH variable.

I can add the given path, and test for the existence of the given path.
I can write expressions that work to detect the given path in $PATH.
What I don't seem able to do is combine all that in a script.

Here's what I have for the script:


# A short script for testing some code.

# pelican seems to like local installations here.
echo Substr is "${SUBSTR}"
# if [[ ! ${PATH} =~ .*/home/charles/.local/bin.* ]] ; then
if [[ $( echo "$PATH" | grep -E -v "${SUBSTR}" ) ]] ; then
     echo Substring NOT found.;

     if [ -d "${HOME}/.local/bin/" ] ; then
         echo Adding to \$PATH
     echo Substring found!;

I call the script with the . operator: ". test.sh"
this is the result:

root@primergy:~/software-env# . test.sh

Substr is /root/.local/bin/
Substring NOT found.

Why do I always end up adding the given path, even if it is already in

Debian is Buster, bash is 5.0.3(1)-release (x86_64-pc-linux-gnu).

Reply to: