Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

is there a way to assign a value to a variable, that value which we get in terminal by writing any command?

Example: command: sensors

we get cpu temperature. How can I assign this value to a temp_cpu variable. Thanks..

share|improve this question
1  
This question is more suited to Super User or to Unix & Linux. Try temp_cpu=$(sensors) (this will turn newlines to spaces, though). You can use grep to filter the specific info you need, too. –  edwin Jul 22 '13 at 22:16
    
@edwin why is the question not so suitable for this place? –  Tshepang Jul 23 '13 at 9:36
    
@Tshepang, this question is not specific to Ubuntu, it's just about the Unix-like shell. Thus, Unix & Linux. –  edwin Jul 23 '13 at 15:53
    
Did the policy/general opinion change? I thought this site welcomed questions that are not necessarily specific to Ubuntu. –  Tshepang Jul 23 '13 at 16:55

1 Answer 1

Yes, you use my_var=$(some_command). For example:

$ foo=$(date)
$ echo $foo
Mon Jul 22 18:10:24 CLT 2013

Or for your specific example, using sed and grep to get at the specific data you want:

$ cpu_temp=$(sensors acpitz-virtual-0 | grep '^temp1:' | sed 's/^temp1: *//;s/ .*//')
$ echo $cpu_temp
+39.0°C
share|improve this answer
    
thanks to both of you. –  eeecoder Jul 23 '13 at 1:31
    
@eeecoder: If this answer was helpful to you, then please consider marking it as the accepted answer so others may more easily find it in the future. This is also a polite way to thank the person answering your question for helping you out. –  minerz029 Jan 24 at 21:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.