0

I used the following way to execute a collection of Bash script files, in the current Bash session:

source ~/myScripts/{assignments.sh,nginx_conf.sh,php_conf.sh,drush_install.sh}

It feels to me uncomfortable to maintain. Some vertical collection is better. Pseudocode:

assignments.sh
nginx_conf.sh
php_conf.sh
drush_install.sh

How would you do that vertically?

By the way I'm not sure that a source here-document like this one is the best way.

Update

I now understand that my one line source operation was doomed to fail because as of Bash 4.3.48(1), the Bash interpreter evaluates source in such a way that it could only work with one file, and any other file beyond it, will be evaluated as an argument for the first file (a brace set {} wouldn't help with this).

I get the impression this is the same as with bash instead source.

5

A heredoc places each filename on its own line without anything else (though assumes that the filenames do not contain anything crazy like a newline) and allows for a specific ordering of the filenames:

while read f; do
   source ~/myScripts/"$f"
done <<SRC_LIST
assignments.sh
nginx_conf.sh
php_conf.sh
drush_install.sh
so_forth.sh
SRC_LIST

this also avoids the problem of source file [arguments] where the subsequent filenames would be treated as arguments to assignments.sh (unless you did mean the subsequent to be arguments??). The list would need to be manually kept up to date with what is on the filesystem.

Another option would be to skip the tedium of listing the files and glob them in; this assumes that all the matching files in the directory can and should be sourced in (so no mixing in other random *.sh files that must not be sourced). However this is complicated by the edge case of when no files are matched by the glob, in which case bash will by default pass the literal filename of ~/myScripts/*.sh in to be sourced, so that must be worked around (temporarily, if necessary) and nothing sourced if there are no matches:

REVERT=$(shopt -p nullglob)
shopt -s nullglob
for f in ~/myScripts/*.sh; do
    source "$f"
done
$REVERT

with this method the filenames would need to be named in a way that the glob matches them in a correct order if there is an order the files need to be sourced in. (In ZSH one would not need the shopt calls as instead for f in ~/myScripts/*.sh(N); do would suffice to perform a null glob. Other shells will vary in how they handle globs and what to do when nothing matches.)

  • Thanks thrig. Please add a few words on why you used read f? And also if the heredoc comes after the done how is it still running? – user9303970 Feb 7 '18 at 8:37
  • read f is how lines are read into a variable. the heredoc is not "running" in any sense; the shell parses it and sends the data it contains to the loop – thrig Feb 7 '18 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.