-1

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
1

2 Answers 2

9

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

2

[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.

You must log in to answer this question.