EMBED YOUR TERMINAL RECORDINGS ONLINE

Output into shellscript variable

DATE: 2011-10-13
AUTHOR: Coder of Salvation
AUTHOR WWW: http://leon.vankammen.eu
RELATED ARTICLE: http://www.gnu.org/s/bash/manual/bash.html

Related recordings:

CAT COMMAND IN LINUX
tags: cat unix gnu linux view file textfile editing
LIST DIRECTORIES IN LINUX WITH LS
tags: ls directory show content linux disk beginner
SUTRAPHP-INTRODUCTION
tags: sutraphp sutra php console cli eventbased
MORE OVERVIEW IN MYSQL WITH SELECTS
tags: mysql, select, more overview, console
M4, THE HIDDEN GNU TREASURE
tags: m4, gnu, macroprocessor, template, search, replace, sed
THE UNIX TREE COMMAND
tags: unix console tree bash directory listing
DEVTODO: COMMANDLINE TODO MANAGEMEN
tags: todo xml tasks productivity management
VIM ALIGN
tags: vim align aligning text text editing text trick
VISUAL SELECTS IN VIM
tags: vim visual selects regex text editing fast
GENERATING PHP & SQL (MAPPING) CODE
tags: sutra generate sql generate database php cms mysql





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

 

Name   
Email (never shown)  
Comment