A
A
Alexey2014-01-30 16:21:13
bash
Alexey, 2014-01-30 16:21:13

What is the difference between $var and ${var}?

Gentlemen, explain to me, unlucky, what is the difference between $var and ${var}?
There is a script that bites a variable from a file and writes it to the database:

filename=/home/pi/adc/work/integral/$(date "+%Y/%m/%Y%m%d.txt")
var=$(tail -1 $filename | head -1 | cut -c26-30 | tr -d ' ')
rrdtool update /home/pi/adc/PIstat.rrd N:$var

The problem is that in one case everything works correctly, and in the other, some completely ridiculous value is written to the database. The solution was to replace $var with ${var} on the last line. But the commands echo $var and echo ${var} show the same result. What's the catch?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vlad Zhivotnev, 2014-01-30
@WildHorn

{} explicitly limits the indication of the place where the name of the variable ended. \
In fact, this is the same thing (in this case).
But with this construction:
echo blah${var1}blah${var2} blah${var3}${var4}var1 you will have problems for yourself.
And there will be problems if you have variables
$var1var2
$var1
And you want to do:
echo ${var1}var2
Well, in general, writing {} is considered good form, even if this is not required in your design - it increases the readability of scripts.

S
svd71, 2014-01-30
@svd71

The catch is that you can't see the difference between the two variables like
echo $VAR ;
echo $varlog;
in the second variant, what exactly is meant: a separate variable $VARLOG or a variable $VAR followed by LOG.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question