This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.
Last Updated: 2024-11-23
The following piece of code has an effect similar to rsync for copying folders with metadata like permissions. But what's exactly is going on with the combination of brackets and parentheses?
(cd src && tar -c .) | (cd dst && tar -xp)
fork
on each area in parentheses and each of these is known
as a subshell. Each fork has its own working directory, own set options, etc.
These processes don't interfere with one another.(echo "hi") > temp.txt && cat temp.txt
you'll see "hi" written(echo "hi")
and $(echo "hi")
. In the
latter case, it fails saying the command "hi" was not found - i.e. the parent shell tries to
execute the returned value. This $()
technique is known as "command
substitution" and it also invokes a subshell.You might want to supress an environmental variable temporarily with a subshell
bash
(unset http_proxy; wget ...)
-c
bottle up argument directory (.) in tar-xp
extracts contents preserving permissions