Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What's the difference between temp=$1 and temp=1? Why do I need the dollar sign?

For example:

#!/bin/bash

temp=$1
cell=$((($temp-32)*5/9))

echo $cell
share|improve this question
1  
mywiki.wooledge.org/BashGuide –  jordanm Feb 20 '12 at 20:54
add comment

migrated from stackoverflow.com Feb 21 '12 at 16:52

This question came from our site for professional and enthusiast programmers.

2 Answers

The dollar-sign triggers parameter expansion; 1 is the string "1", while $1 is the first shell parameter.

share|improve this answer
add comment

[In addition to what ruakh had already mentioned]

Your script attempts to convert a temperature value from Fahrenheit to Celsius scale. You could save your file with a name say, tocelsius.sh. Now, you can convert any given temperature (in Fahrenheit scale) by passing a value to your script:

bash tocelsius.sh 66

When you do so, your $1 would contain the value 66. Thus, you would get the corresponding output.

However, when you write temp=1, the variable temp will always take the fixed value 1. No argument passed to your script (as shown above) would be considered.

To conclude, $xyz points to the value of the variable xyz. $1, $2, ... are special cases where it points to the arguments passed from command line, if any.

share|improve this answer
add comment

Your Answer

 
discard

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