bash:assign_output_of_shell_command_to_variable
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
bash:assign_output_of_shell_command_to_variable [2021/01/26 11:41] – [Example using Date] peter | bash:assign_output_of_shell_command_to_variable [2021/01/26 14:24] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== BASH - Assign Output of Shell Command To Variable ====== | ||
- | |||
- | To assign output of any shell command to variable in bash, use the following command substitution syntax: | ||
- | |||
- | <code bash> | ||
- | var=$(command-name-here) | ||
- | var=$(command-name-here arg1) | ||
- | var=$(/ | ||
- | var=$(/ | ||
- | </ | ||
- | |||
- | ...or use backticks based syntax as follows to assign output of a Linux command to a variable: | ||
- | |||
- | <code bash> | ||
- | var=`command-name-here` | ||
- | var=`command-name-here arg1` | ||
- | var=`/ | ||
- | var=`/ | ||
- | </ | ||
- | |||
- | Do not put any spaces after the equals sign and command must be on right side of =. | ||
- | |||
- | <WRAP important> | ||
- | **NOTE:** The use of **$(command)** is not portable. | ||
- | </ | ||
- | |||
- | |||
- | ---- | ||
- | |||
- | ===== Examples ===== | ||
- | |||
- | To store date command output to a variable called now, enter: | ||
- | |||
- | <code bash> | ||
- | now=$(date) | ||
- | </ | ||
- | |||
- | or | ||
- | |||
- | <code bash> | ||
- | now=`date` | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | To display back result (or output stored in a variable called $now) use the echo or printf command: | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | printf " | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | Wed Apr 25 00:55:45 IST 2012 | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | You can combine the echo command and shell variables as follows: | ||
- | |||
- | <code bash> | ||
- | echo "Today is $now" | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | Today is Wed Apr 25 00:55:45 IST 2012 | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | You can do command substitution in an echo command itself (no need to use shell variable): | ||
- | |||
- | <code bash> | ||
- | echo "Today is $(date)" | ||
- | printf "Today is %s\n" " | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | Today is Wed Apr 25 00:57:58 IST 2011 | ||
- | </ | ||
- | |||
- | |||
- | ---- | ||
- | |||
- | ===== Use Multiline Command ===== | ||
- | |||
- | Try the following syntax: | ||
- | |||
- | <code bash> | ||
- | my_var=$(command \ | ||
- | arg1 \ | ||
- | arg2 \ | ||
- | arg3 ) | ||
- | echo " | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Example using Date ===== | ||
- | |||
- | <code bash> | ||
- | OUT=$(date \ | ||
- | --date=' | ||
- | echo " | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Example using Ping ===== | ||
- | |||
- | <code bash> | ||
- | #!/bin/bash | ||
- | _ping="/ | ||
- | domain=" | ||
- | |||
- | ping_avg=" | ||
- | -q \ | ||
- | -c 4 \ | ||
- | ${domain} | grep rtt)" | ||
- | |||
- | echo "Avg ping time for ${domain} : ${ping_avg}" | ||
- | </ | ||
bash/assign_output_of_shell_command_to_variable.1611661279.txt.gz · Last modified: 2021/01/26 11:41 by peter