0

I have the following line in my .bashrc file:

cat "$HOME/.module_list" | while read m; do echo "Loading module $m..."; module load "$m"; done

where $HOME/.module_list is a file that contains the name of a module to load on each row. When .bashrc is sourced, the line goes through each module in the list one by one and seemingly loads them, but afterwards, when I do module list, none of the modules are loaded.

Does this line create a new scope into which the modules are loaded, which is then terminated as soon as the line finishes, or why aren't the modules still loaded afterwards? How can I successfully load all modules from the list and still have them be loaded afterwards?

3

1 Answer 1

1

A simple solution is to load your modules names into an array:

#!/bin/bash

readarray -t modules < ~/.module_list

module load "${modules[@]}"

An other solution is to use a while read loop, making sure to avoid the |:

#!/bin/bash

while IFS='' read -r m
do
    module load "$m"
done < ~/.module_list
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, thank you! In the second alternative, which command is it that gets ~/.module_list as input? I'm not that familiar with the < syntax.
The whole while loop (i.e. each command in it) will "share" the content of ~/.module_list as input; in this aspect, it's 100% equivalent to cat ~/.module_list | while
Also, what is IFS in the second alternative?
It's not really needed here, it's just a habit of mine. Setting IFS to the empty string in the read command is for making sure that the leading/trailing blanks characters are not stripped from the line

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.