buzzin
buzzin

Reputation: 141

echo command with variable and string

I have 3 variables I'm trying to insert into an echo command that contains the string, as well.

Here is what I have:

1= "test1"
2= "test2"
3= "test3"

FileName= "WEATHERMAP"_"$1"_"STATE"_"$2"_"CITY"_"$3" 

echo $FileName

I want it to echo WEATHERMAP_test1_STATE_test2_CITY_test3
Instead I get WEATHERMAP__STATE__CITY_

I know this has something to do with the underscore, unfortunately, I need the underscore.

The only examples I have seen are putting two variables together, or it started with a variable followed by a string.

Upvotes: 5

Views: 31129

Answers (3)

Sergey Brodin
Sergey Brodin

Reputation: 361

The numerical variables are reserved for shell internal purposes, $0 contains script filename, $1 - first script argument, like ./script.sh first-argument, $2 - second script argument, etc.

Upvotes: 0

riteshtch
riteshtch

Reputation: 8769

Don't start variable names with a number.

$ a="test1"
$ b="test2"
$ c="test3"
$ FileName="WEATHERMAP_${a}_STATE_${b}_CITY_${c}"
$ echo "$FileName"
WEATHERMAP_test1_STATE_test2_CITY_test3
$

Upvotes: 3

Saravanan Sivam
Saravanan Sivam

Reputation: 49

I'm guessing you're using bash by default, in which case letters, numbers and underscores are allowed, but you can't start the variable name with a number. You can achieve by following code :-

a="test1"
b="test2"
c="test3"

FileName="WEATHERMAP"_"$a"_"STATE"_"$b"_"CITY"_"$c"
## Also, can assign as below :
## FileName="WEATHERMAP_${a}_STATE_${b}_CITY_${c}"

echo $FileName

Upvotes: 0

Related Questions