-1

No idea what's happening here. I am on Bash 3.2 on a Mac. I have:

ores_resource(){
  for f in "$(cd "$HOME/.oresoftware/bash" && find . -type f)"; do
       f="${f:2}"
       echo "my file $f"
  done;
}

I get this echoed out:

my file r2g.sh
./fame.completion.sh
./read_json.sh
./nlu.sh
./lmx.sh
./public-bash-utils.sh
./run-tsc-if.sh
./r2g.completion.sh
./fame.sh
./waldo.sh
./nlu.completion.sh

so what the heck is going on here - I thought it would log out "my file" in front of each of the lines?

  • 2
    `"$(cd "$HOME/.oresoftware/bash" && find . -type f)"` is a single string: see [Bash Pitfall #1](https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29) and [Why is looping over find's output bad practice?](https://unix.stackexchange.com/questions/321697/why-is-looping-over-finds-output-bad-practice) – steeldriver Jul 11 '19 at 21:39
  • use `xargs` command or `--exec` option. – ctrl-alt-delor Jul 11 '19 at 21:42
  • i am a newb can someone show me something that works por favor –  Jul 11 '19 at 21:44

1 Answers1

2

Here is a solution using find with the -exec option:

ores_resource() {
  cd $HOME/.oresoftware/bash
  find . -type f -exec bash -c 'echo "my file ${1:2}"' bash {} \;
}

For each filename found by find a new bash process is started with the command string following -c. Each filename {} is passed as argument $1 to the bash process (like your variable $f).

Freddy
  • 25,172
  • 1
  • 21
  • 60