bashenvironment-modules

How to execute command with partial result from another command?


I have:

$ module avail firefox --latest
---------------------------------------------------------- /env/common/modules -----------------------------------------------------------
firefox/83.0

I would like to extract the firefox/83.0 part of the above result into a new command:

$ module load firefox/83.0

This is what I have so far, but it does not seem like I can pipe the result from the grep onto the next command:

$ module avail firefox --latest | grep firefox | echo module load 

Note: this is on a version of Modules which does not have the module load app/latest ability.


Solution

  • Use a regex to capture only firefox/83.0, then use command substitution to use that result in the next command;

    module load $(module avail firefox --latest | sed -nre 's/^firefox\/[^0-9]*(([0-9]+\.)*[0-9]+).*/\0/p'
    

    More info on the regex: How to extract a version number using sed?


    Using xargs;

    module avail firefox --latest | sed -nre 's/^firefox\/[^0-9]*(([0-9]+\.)*[0-9]+).*/\0/p' | xargs module load