The scl enable ... command creates a new shell; you've probably ended up with several nested layers of shells if you've been testing this. If you exit, you'll probably see the gcc --version output from the base system. See how deeply-nested you are with something like pstree -s $$.
To run gcc --version with scl, just put the command on the scl line:
scl enable devtoolset-9 'gcc --version'
Reference: The Red Hat Developer Toolset 9 User Guide (pdf).
Answer from Jeff Schaller on Stack ExchangeThe scl enable ... command creates a new shell; you've probably ended up with several nested layers of shells if you've been testing this. If you exit, you'll probably see the gcc --version output from the base system. See how deeply-nested you are with something like pstree -s $$.
To run gcc --version with scl, just put the command on the scl line:
scl enable devtoolset-9 'gcc --version'
Reference: The Red Hat Developer Toolset 9 User Guide (pdf).
to get the script to act as you want it to, you need the scl command to modify the current bash session rather than spawn a new one.
you can do it this way
#!/usr/bin/env bash
# switch to GCC9 environment for the duration of the script
source scl_source enable devtoolset-9
gcc --version