Ad
Bash Find Or Xargs Evaluates Variables And Subshells Only Once
I noticed that find ... -exec ... {} \;
or xargs -i ... {}
seems to evaluate variables or subshells (like $RANDOM
or $(uuidgen)
) only once, even the command was executed mutiple times.
For example:
$ find . -type f -name \*.txt -exec echo "$RANDOM {}" \;
28855 ./foo/bar.txt
28855 ./foo/bar1.txt
28855 ./foo/bar2.txt
28855 ./foo/bar3.txt
28855 ./foo/bar4.txt
$ grep -lr SOME_TEXT --include=\*.txt | xargs -i echo "$RANDOM {}"
6153 ./foo/bar.txt
6153 ./foo/bar1.txt
6153 ./foo/bar2.txt
6153 ./foo/bar3.txt
6153 ./foo/bar4.txt
Is there a way to get a result like below?
1543 ./foo/bar.txt
543 ./foo/bar1.txt
57224 ./foo/bar2.txt
3525 ./foo/bar3.txt
18952 ./foo/bar4.txt
Ad
Answer
Yes. The variable expansion is performed after the line has been accepted, but before it has been executed. This means that the command that ends up being executed is
'/usr/bin/find' '.' '-type' 'f' '-name' '*.txt' '-exec' 'echo' '28855 {}' ';'
Two basic ways around this:
Use another
bash
that will delay the execution:find . -type f -name \*.txt -exec bash -c 'echo "$RANDOM {}"' \;
Use a loop:
for file in $(find . -type f -name \*.txt -print) do echo "$RANDOM $file" done
If your files have spaces, you have to do something different to preserve them:
mapfile -d '' files < <(find . -type f -name \*.txt -print0) for file in "${files[@]}" do echo "$RANDOM $file" done
Ad
source: stackoverflow.com
Related Questions
- → "Box: laravel/homestead-7" producing "sh.exe":box:: command not found"
- → Retrieve user's image selection from website
- → Getting error when using default task in gulp, shows the following error in Gitbash : Task requires a name that is string
- → Homestead Installaton
- → bash: homestead: command not found
- → Troubles with Artisan Tinker encoding
- → How can I access declared variable in bash when executing Laravel Envoy task?
- → coinex exchange API and use Curl /BASH to Place a market order crypto pair
- → Coiex API Curl Could not resolve host
- → How to GREP words, not lines, that contain specific characters, and print entire word
- → Cmd and Git bash have a different result when run a Python code
- → How can I convert ip ranges formatted in ip1-ip2 to cidr format?
- → Pushing to another user's remote repository without the use of pull requests
Ad