1

I want to source files in a directory, I tried to make it work with this loop :

ls -1 $HOME/.bash_complete.d |while read f; do
    . $HOME/.bash_complete.d/$f
done

though the scripts seem to be executed, any export foo=bar in those are not present in env

Its for counterpart below :

for f in $HOME/.bash_complete.d/*; do
    . $f
done

works as I expect. Is there a scope involved ?

r---------k
  • 947
  • 9
  • 18

2 Answers2

1

See http://mywiki.wooledge.org/BashFAQ/024 and the answer to Bash Script: While-Loop Subshell Dilemma (which this question is a duplicate of), everything in the while loop is executed in a subshell, so it has it's own scope. variables declared in this scope will not be visible from outside the scope.

Community
  • 1
  • 1
glenda
  • 58
  • 2
0

The -l is not required, which gives a long listing of the files. All that is required is the name of the files, which can be obtained by a simple ls command

ls $HOME/.bash_complete.d |while read f; do
    echo $HOME/.bash_complete.d/$f
    $HOME/.bash_complete.d/$f
done
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52