Welcome to the Linux Foundation Forum!

Variable being interpreted as a command in a bash script

theolinux Posts: 4
edited May 15 in Command Line/Scripting


I have a bash script to do some security tasks for new Ubuntu or Red Hat based servers.

As one of the steps, if epel isn't supported, I add it to facilitate installation of fail2ban.

I collect the information needed and store it in variables:

# Determine architecture and store in "arch" variable
arch=$( /bin/arch );
# Determine major release and store in "release" variable
osRelease=$( cat /etc/*os-release | grep PRETTY_NAME | cut -d " " -f 5 | cut -d "." -f 1 );

I then use the variables in a command to support epel:

sudo "subscription-manager repos --enable codeready-builder-for-rhel-"$(osRelease)"-"$(arch)"-rpms"

When the command runs, the place where osRelease should be is empty and when it runs, I get an osRelease: command not found error when it reaches that line in the script:

./first-ten.sh: line 337: release: command not found
Error: 'codeready-builder-for-rhel--x86_64-rpms' does not match a valid repository ID. Use "subscription-manager repos --list" to see valid repositories.

Note that the number 9 should be between the hyphens between rhel and x86_64, but it is empty.

When I run echo $osRelease, I get the number 9, which is correct.

I believe I'm missing something simple but have not been able to figure it out yet. Any insight is much appreciated!

Full script is available on github here: https://github.com/TedLeRoy/first-ten-seconds-redhat-ubuntu/blob/master/first-ten.sh

Any thoughts on script improvements are most welcome too.


Best Answer

  • theolinux
    theolinux Posts: 4
    Answer ✓

    Got it working. Had to get rid of the ()'s and some of the "'s. Working line:
    sudo subscription-manager repos --enable codeready-builder-for-rhel-"$osRelease"-"$arch"-rpms


  • theolinux
    theolinux Posts: 4

    In the captured text, it says release: command not found, but I changed the variable name to osRelease in case release is a system variable. Same result. It now says osRelease: command not found. Sorry for any confusion this may cause.

  • theolinux
    theolinux Posts: 4

    Run of setting the variables and running the relevant line from the script:

    [tedl@localhost ~]$ osRelease=$( cat /etc/*os-release | grep PRETTY_NAME | cut -d " " -f 5 | cut -d "." -f 1 );
    [tedl@localhost ~]$ echo $osRelease
    [tedl@localhost ~]$ arch=$( /bin/arch );
    [tedl@localhost ~]$ echo $arch
    [tedl@localhost ~]$ sudo subscription-manager repos --enable codeready-builder-for-rhel-"$(osRelease)"-"$(arch)"-rpms
    -bash: osRelease: command not found
    [sudo] password for tedl:
    Error: 'codeready-builder-for-rhel--x86_64-rpms' does not match a valid repository ID. Use "subscription-manager repos --list" to see valid repositories.

    The arch variable populates and works fine while osRelease does not, and osRelease is interpreted as a command.


Upcoming Training