Yes you are saving memory. By calling the command exec
you are replacing sh
with the command (xterm
in your example). So you are saving the memory sh
would use.
The question is, how much do you save?
On my system (Ubuntu 14.04, amd64) sh
is a dash and needs 464 kB residential memory and about 4.5 MB virtual memory. Of course virtual memory includes shared libraries, which are only loaded once and are probably loaded anyway for some other process. So effectively I could save about 0.5 MB. At the moment I have 10 instances of sh -c
running, so that would be about 5 MB. Not all that much. Even if it were 100 instances, it would still only be 50 MB. It may be a concern on very minimal or old systems with less then a gigabyte of memory, but then I don't think you'll have much success running 100 separate applications on your desktop there.
If /bin/sh
is a bash
, it would take about 2 MB residential memory (on my system). Granted that is quite a bit more, but still, unless you start huge amounts of programs it is unlikely to matter on a modern system.
That being said, I see no reason not to use exec exec
for simple commands (only one command and its parameters). Obviously you cannot use any shell constructs that way, as the shell is replaced after the first command and any further command will never be run. (for example exec sleep 10; xclock
or exec sleep 10 && xclock
; both will wait for 10 seconds, but xclock
will not be executed)