Score:0

not a valid identifier while exporting path

xk flag

I have two Cuda installations namely 11.8 and 12, and would like to switch between them without manually editing the .bashrc file and sourcing it again.

For that I have come up with the following function placed inside .bashrc

export CUDA_11=/usr/local/cuda-11.8/
export CUDA_12=/usr/local/cuda-12.0/


#Default Cuda12.0 will be loaded, use this to unload 12 and load 11.8

function switchCuda { 
 local pathToUnset=$1"bin/"
 echo $pathToUnset
 local lib64ToUnset=$1"lib64/"
 local pathToSet=$2"bin/"
 local ldPathToSet=$2"lib64/"
 local currentPath=:$PATH:
 unset PATH
 local removedPath=${currentPath/:$pathToUnset:/:}
 removedPath=${removedPath%:}
 removedPath=${removedPath#:}
 PATH=$removedPath
 export $PATH

 currentPath=:$LD_LIBRARY_PATH:
 unset LD_LIBRARY_PATH
 local modifiedLDPath=${currentPath/:$lib64ToUnset:/:}
 modifiedLDPath=${modifiedLDPath%:}
 $modifiedLDPath=${modifiedLDPath#:}

 LD_LIBRARY_PATH=$modifiedLDPath
 export $LD_LIBRARY_PATH
 #echo "I AM DONE UNSETTING THE PATH" 
 if [$2 == "" ]; then
     echo "Simply unsetting cuda path, as provided cuda path is empty. use loadCuda to load the desired path"; return;
fi
    
 export PATH=$PATH:$2
 export PATH=$PATH:$pathToSet
 export LD_LIBRARY_PATH=$ldPathToSet${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
 }

export -f switchCuda

function loadCuda { 
 export PATH=$PATH:$1
 export PATH=$PATH:$1bin
 export LD_LIBRARY_PATH=$1lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
 }

export -f loadCuda

The intended result of the above function should be as follows -

#in a fresh terminal
nvcc --version (should result in 12, as by default cuda 12 is added to path)

switchCuda ${CUDA_12} ${CUDA_11}

nvcc --version (should be 11.8, as cuda 11.8 will now be in path, nobody should be able to find 12)

However, when I call this function from the command line, I get the first error is as follows -

bash: export: `/opt/ros/noetic/bin:/home/atharva/ros_catkin_pkg/install_isolated/bin:/opt/intel/oneapi/vtune/2022.2.0/bin64:/opt/intel/oneapi/vpl/2022.1.0/bin:/opt/intel/oneapi/mpi/2021.6.0//libfabric/bin:/opt/intel/oneapi/mpi/2021.6.0//bin:/opt/intel/oneapi/mkl/2022.1.0/bin/intel64:/opt/intel/oneapi/itac/2021.6.0/bin:/opt/intel/oneapi/intelpython/latest/bin:/opt/intel/oneapi/intelpython/latest/condabin:/opt/intel/oneapi/inspector/2022.1.0/bin64:/opt/intel/oneapi/dpcpp-ct/2022.1.0/bin:/opt/intel/oneapi/dev-

utilities/2021.6.0/bin:/opt/intel/oneapi/debugger/2021.6.0/gdb/intel64/bin:/opt/intel/oneapi/compiler/2022.1.0/linux/lib/oclfpga/bin:/opt/intel/oneapi/compiler/2022.1.0/linux/bin/intel64:/opt/intel/oneapi/compiler/2022.1.0/linux/bin:/opt/intel/oneapi/clck/2021.6.0/bin/intel64:/opt/intel/oneapi/advisor/2022.1.0/bin64:/home/atharva/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:

/usr/local/cuda-12.0:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/:/usr/local/cuda-11.8/:/usr/local/cuda-11.8/+bin/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/': **not a valid identifier**

The last line of the above error message is as follows -

12.0/bin:/home/atharva/TensorRT/TensorRT/bin/': **not a valid identifier**

I believe there are multiple issues -

  1. String substitution is not happening correctly
  2. cuda_12 is still coming in path, even after removing it.
  3. Also, there is another issue, Assume I run switchCuda {CUDA_12} "". It should completely remove cuda, and doing something like nvcc --version should result in command not found. However, it is still able to find nvcc. Why so ? How can I achieve my intended result?

TIA

hr flag
`export $PATH` should be `export PATH`
hr flag
... for other issues [www.shellcheck.net](https://www.shellcheck.net/)
Atharva Dubey avatar
xk flag
Thanks a lot @steeldriver, it worked !
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.