Sean Sean - 29 days ago 5
Bash Question

g++: "command not found" after declaring variables in .bash

My g++ "quit working" after I declared variables before the lines which use g++.

Ex: This works.

echo "Train LINE starts"
g++ -lm -pthread -Ofast -march=native -Wall -funroll-loops -ffast-math -Wno-unused-result line.cpp -o line -lgsl -lm -lgslcblas


But this does NOT.

PATH="foo path"
echo "Train LINE starts"
g++ -lm -pthread -Ofast -march=native -Wall -funroll-loops -ffast-math -Wno-unused-result line.cpp -o line -lgsl -lm -lgslcblas


Instead, I got the error "g++: command not found". I am sure my variable declaration is valid?

Answer

The PATH environment variable consists of a colon-delimited list of directories. The shell searches for executable files in these directories in response to commands issued by a user.

For example, if g++ executable is located in /usr/bin directory, then you can implicitly call /usr/bin/g++ only if PATH contains /usr/bin:

echo $PATH
/usr/bin:/bin:/opt/bin

If you want to add a search path, you should append it to the existing value of the variable:

PATH="$PATH:/new/search/path"