0
Thanks

A few words of thanks would be greatly appreciated.

Retrieve the result of command in a variable







For specific reason, it is necessary to retrieve the result of a command (or back code) in variable to exploit the script later.

Below are different examples:

Syntax

var = $ (command)   

Note: Please note that the old method using reverse quotes ('command') is however forbidden and the examples presented here are better.


Examples
  • Recovery of standard out
    • stdout only
      • var=$(command)
    • stdout and stderr
      • var=$(command 2>&1)
  • Recovery back code ($?)
    • back code only
      • command
      • var=$?
    • Both
      • var=$ (command)
      • var=$?
0
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « Retrieve the result of command in a variable », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).

Subscribe To Our Newsletter!

The Best of CCM in Your Inbox

Subscribe To Our Newsletter!