EMBED THIS RECORDING:
<div><script id="playterm-MjAxMS0xMC9zaGVsbHNjcmlwdHZhcnR0eXJlYy0xMzE4NDU5MTk2fDgweDI0" type="text/javascript" src="https://playterm.org/js/?hash=MjAxMS0xMC9zaGVsbHNjcmlwdHZhcnR0eXJlYy0xMzE4NDU5MTk2fDgweDI0" class="size:80x24"></script></div>
I made this shell recording to show how to redirect output into
variables.
method #1
=========
MYVAR=`pwd`
method #2
=========
MYVAR=$(pwd)
During scriptingexperiences in the past I noticed method #1 is very slow. In this recording I also
experimented a bit with the command 'time', and it turned out to be true.
Anybody got an idea why?
Maxim said
I have no idea what you are talking about. I read your remark above and I became curious. I tried to
reproduce your findings in both Bash and zsh, but I cannot find any speed differences between using
$() and ``. I prefer using $() though, it's way more obvious that `` in scripts.
Coder of Salvation said
Well I just tested it again, but maybe you just pointed out its an problem related to my specific
architecture/linux distro. Check these tests:
$ time BLA=`ls -laR /tmp`
real 0m2.694s
user 0m0.316s
sys 0m0.368s
$ time BLA=$(ls -laR /tmp)
real 0m0.582s
user 0m0.252s
sys 0m0.288s
Anyways, thanks for pointing out!
Markus said
You really shouldn't take "ls" to benchmark anything. Once "ls -laR /tmp" has
run, the system is caching the result and thus the second call is faster. It doesn't matter if it's
$() or ``. Execute `` before $() and you'll see.
So you should take a command (or a snippet) which always takes a fixed amount of time, like:
$ time FOO=`for (( N=10000; N--; )); do echo "test" > /dev/null; done`
real 0m0.355s
user 0m0.226s
sys