Search code examples
shellfindxargs

How to get value of {} in shell?


find . -name "*.network" -o -name "*.index" | xargs -n 1 -I {} -P 5 sh -c "ncols=3; filename={}; echo $filename"

I want get the filename stored in a variable. By setting filename={}, and echo filename, I got nothing output in console.

Since I want to use multi-thread. xargs is necessary in my script.

I use single quotes as suggested by aicastell. But I want to used awk inside quotes. What should I do with single quotes inside quotes? \s did not work.

Can anybody help me with this ?


Solution

  • Since $filename is within double quotes, the shell substitues it before it already before it runs your pipe. With other words: The filename in the echo statement refers to the variable in the calling shell, not in the subshell which you open with sh -c.

    Hence, use single quotes instead!