-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?

3

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).

-1

Not sure if this is robust, but I just used backticks like so:

ores_resource(){
  for f in `(cd "$HOME/.oresoftware/bash" && find . -type f)`; do
       f="${f:2}"
       echo "my file $f"
  done;
}
  • This will break as soon as any file has a space, tab, or newline in it. – Jeff Schaller Jul 18 at 13:07

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.