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 Exchange
🌐
Stack Overflow
stackoverflow.com › questions › 61713954 › how-to-use-devtoolset-in-autotools
linux - How to use devtoolset in autotools - Stack Overflow
I know the option scl enable devtoolset-9 'make' but want a mechanism to enable it from configure script. I tried multiple options like setting up SHELL, CC variable as CC='source scl_source enable devtoolset-9; gcc' ...
Discussions

linux - How to permanently enable newer version of software installed from SCL repo? - Unix & Linux Stack Exchange
On CentOS 6.4: I installed a newer version of devtoolset (1.1) and was wondering how I would go about permanently setting these to be default. Right now, when I ssh into my server running CentOS 6, I More on unix.stackexchange.com
🌐 unix.stackexchange.com
May 8, 2020
Scl enable devtoolset-7 bash on Rocky 9
I didn’t find scl or devtoolset-7 to install. How should I do? Thank you very much. More on forums.rockylinux.org
🌐 forums.rockylinux.org
16
0
March 9, 2023
centos7 - Enable devtoolset-8 for zsh on Centos 7 - Stack Overflow
I run Centos 7, and I installed devtoolset-8 in order to get gcc 8. Here I found how to enable it for bash, which is done simply by adding source scl_source enable devtoolset-8 to .bashrc. If I ap... More on stackoverflow.com
🌐 stackoverflow.com
linux - Can not find required gcc version after devtoolset installation - Stack Overflow
When I try scl enable devtoolset-8 -- bash, it shows · Unable to open /etc/scl/conf/devtoolset-8! Then I have checked in /etc/scl/conf/ where there is one file named 'devtoolset-9'. But I was expecting devtoolset-8. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Posit Support
support.posit.co › hc › en-us › articles › 360006145413-RStudio-Workbench-RStudio-Server-Pro-with-devtoolset-enabled
Posit Workbench with devtoolset enabled – Posit Support
July 25, 2024 - yum install centos-release-scl · yum install devtoolset-9 · Execute: gcc --version · Enable devtoolset-10 with: source scl_source enable devtoolset-9 · Review gcc version now with: gcc --version · Once tested, you can implement this on ...
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360004312319-enable-scl-devtoolset
enable scl devtoolset – IDEs Support (IntelliJ Platform) | JetBrains
1. I moved all my configuring code from .bashrc to .profile because (I don't remember exactly when) sometimes .bashrc was not applied to the environment. so for now devtoolset in .profile · 2. the command is "source scl_source enable devtoolset-4" .
Top answer
1 of 5
93

In your ~/.bashrc or ~/.bash_profile Simply source the "enable" script provided with the devtoolset. For example, with the Devtoolset 2, the command is:

source /opt/rh/devtoolset-2/enable

or

source scl_source enable devtoolset-2

Lot more efficient: no forkbomb, no tricky shell

2 of 5
17

An alternative of source /opt/rh/devtoolset-4/enable is

source scl_source enable devtoolset-4

The above shell script scl_source is more elegant than using a hard coded path (may be different on another machine). However scl_source does less because /opt/rh/devtoolset-4/enable uses scl_source and other stuff.

To use scl_source you may have to upgrade package scl-utils

yum update scl-utils  # old scl-utils versions miss scl_source

Quick copy-paste

echo 'source scl_source enable devtoolset-4' >> ~/.bashrc
    # Do not forget to change the version ↑

Source code for curious people

An example of scl_source source code:
https://gist.github.com/bkabrda/6435016

The scl_source installed on my Red Hat 7.1

#!/bin/bash

_scl_source_help="Usage: source scl_source <action> [<collection> ...]

Don't use this script outside of SCL scriptlets!

Options:
    -h, --help    display this help and exit"

if [ $# -eq 0 -o $1 = "-h" -o $1 = "--help" ]; then
    echo "$_scl_source_help"
    return 0
fi


if [ -z "$_recursion" ]; then
    _recursion="false"
fi
if [ -z "$_scl_scriptlet_name" ]; then
    # The only allowed action in the case of recursion is the same
    # as was the original
    _scl_scriptlet_name=$1
fi
shift 1

if [ -z "$_scl_dir" ]; then
    # No need to re-define the directory twice
    _scl_dir=/etc/scl/conf
    if [ ! -e $_scl_dir ]; then
        _scl_dir=/etc/scl/prefixes
    fi
fi

for arg in "$@"; do
    _scl_prefix_file=$_scl_dir/$arg
    _scl_prefix=`cat $_scl_prefix_file 2> /dev/null`
    if [ $? -ne 0 ]; then
        echo "Can't read $_scl_prefix_file, $arg is probably not installed."
        return 1
    fi

    # First check if the collection is already in the list
    # of collections to be enabled
    for scl in ${_scls[@]}; do
        if [ scl ]; then
            continue 2
        fi
    done

    # Now check if the collection isn't already enabled
    /usr/bin/scl_enabled $arg > /dev/null 2> /dev/null
    if [ $? -ne 0 ]; then
        _scls+=($arg)
        _scl_prefixes+=($_scl_prefix)
    fi;
done

if [ $_recursion == "false" ]; then
    _i=0
    _recursion="true"
    while [ {#_scls[@]} ]; do
        _scl_scriptlet_path="${_scl_prefixes[$_i]}/${_scls[$_i]}/${_scl_scriptlet_name}"
        source "$_scl_scriptlet_path"
        if [ $? -ne 0 ]; then
            echo "Can't source $_scl_scriptlet_name, skipping."
        else
            export X_SCLS="${_scls[$_i]} $X_SCLS"
        fi;
        _i=_i+1))
    done
    _scls=()
    _scl_prefixes=()
    _scl_scriptlet_name=""
    _recursion="false"
fi
🌐
JLab Wiki
wiki.jlab.org › epsciwiki › index.php › Using_devtoolset-9_on_your_RHEL7_desktop › laptop
Using devtoolset-9 on your RHEL7 desktop/laptop - epsciwiki
sudo yum install devtoolset-9 · To use the tools from a given toolset, you need to launch a new shell with those enabled via: scl enable devtoolset-9 tcsh · You will need to do this for every shell you use with the toolset.
Find elsewhere
🌐
University of Edinburgh
computing.help.inf.ed.ac.uk › scl
Software Collections | Documentation
To activate the devtoolset you ... You can run any application or script you wish, it does not need to be a shell. The scl enable command is used to enable the specified software collection....
🌐
GitHub
gist.github.com › superzscy › ea619f881c92b8cdae8faaf782d0f031
Installing-GCC-9-on-CentOS-7.md · GitHub
yum install -y centos-release-scl yum install -y devtoolset-9 scl enable devtoolset-9 bash · If you restart the shell, it will revert to GCC 4.8.5. You can add "source /opt/rh/devtoolset-9/enable" into you bashrc file to set GCC 9 as the default.
🌐
GitHub
gist.github.com › nchaigne › ad06bc867f911a3c0d32939f1e930a11
Building GCC 9.2.0 on CentOS 7 · GitHub
yum install centos-release-scl -y yum clean all yum install devtoolset-9-* -y scl enable devtoolset-9 bash
Top answer
1 of 1
1

Copying comments into the semblance of an answer.

Change the sh script_1.sh etc lines to bash -x script_1.sh (or sh -x script_1.sh since the scripts don't seem to use any Bash-specific syntax) and monitor what's going on. Do you see the version information from gcc --version in script_1.sh?

gcc --version is only printed when I comment out scl enable devtoolset-9 bash. I ran scl enable devtoolset-9 bash and it does not output anything to the screen.

That suggests the scl command is not completing. Maybe it is waiting for input from the terminal. Do you see the output from which gcc when you include the scl command? If not, then it is close to certain that scl is trying to read from the terminal. I dunno what it's reading — it isn't a command I'm familiar with.

It is not waiting for any input. After execution, it brings the prompt again when I run it by itself.

If you're not seeing the which gcc and gcc --version output, then it is probable that the scl command is not completing, IMO. What does the bash at the end of the command options do? Does it run a bash process? If so, where is its input coming from? Running with the -x option (sh -x script_1.sh) would show you what is being executed, and whether scl is completing.

scl enable foo bar bash actually runs a bash instance with foo and bar Software Collections enabled. See https://linux.die.net/man/1/scl

OK; and what is that bash instance doing? Is it not waiting for input before it executes anything? It's a little surprising that there isn't a prompt, but not completely astonishing. Have you tried typing exit when scl hangs?

I just tried scl enable devtoolset-9 bash & echo "Enabling devtoolset-9" and it works and ultimately prints out the gcc --version.

Well, that & runs the scl command in background, leaving the echo to run, and then which gcc and gcc --version. Replace the & with a semicolon. Or replace the & with -c 'echo Hi' and a semicolon and see what happens.

Wonderful! Adding -c echo "Hi" made it work!

So that bash command specified at the end of scl enable devtoolset-9 bash was waiting for input from you, which is why it didn't terminate (and you don't see which gcc running) and so on. You've got the same issue at the end of script_2.sh — what about the other scripts? But you now know what's going on and can decide what to do about it. Using -c exit would simply terminate the shell (instead of echoing Hi), for example.

I'd need to study the scl command more, but do you actually need to use it in these scripts?

🌐
Stack Overflow
stackoverflow.com › questions › 45821749 › rhel-6-scl-script-not-sourcing-for-normal-user
redhat - RHEL 6 SCL script not sourcing for normal user - Stack Overflow
August 22, 2017 - Now on this new 6.9 install (on real hardware) I've tried the same script in the same location, but it never sources. New terminal windows always default to the system's gcc 4.4. I can, however, manually source the enable script and it does work: $ gcc --version gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18) $ source scl_source enable devtoolset-2 $ gcc --version gcc (GCC) 4.8.2 20140120 (Red Hat 4.8.2-15)
🌐
Softwarecollections
softwarecollections.org › en › scls › rhscl › devtoolset-8
Developer Toolset 8 — Software Collections
Install the collection: $ sudo yum install devtoolset-8 # 3. Start using software collections: $ scl enable devtoolset-8 bash