# Question on using a variable in KSH

**URL:** <https://community.unix.com/t/question-on-using-a-variable-in-ksh/272993>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [September 8, 2010, 10:47am UTC](https://community.unix.com/t/question-on-using-a-variable-in-ksh/272993 "2010-09-08T10:47:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chatguy](https://community.unix.com/letter_avatar/chatguy/32/5_5575768a8748004e209b776fc1b2916d.png) [@chatguy](https://community.unix.com/u/chatguy)\
**Post date:** [September 8, 2010, 10:47am UTC](https://community.unix.com/t/question-on-using-a-variable-in-ksh/272993/1 "2010-09-08T10:47:00Z")

</div>

Hi all,

The below command tries to copy ".tgz" instead of "hello\_test.tgz" -- It seems as if the underscore gets in the way. I tried with different ways of using quotes, with no luck, unfortunately...it's probably very simple, but may I ask how this would be done:

How would the below be fixed to copy "hello\_test.tgz" instead of ".tgz"?

```nohighlight
NAME=hello
cp $NAME_test.tgz /tmp/$NAME_test.tgz

```

Thanks very much  
cg

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [September 8, 2010, 10:55am UTC](https://community.unix.com/t/question-on-using-a-variable-in-ksh/272993/2 "2010-09-08T10:55:01Z")

</div>

By using:

```nohighlight
cp -- "${NAME}_test.tgz" "/tmp/${NAME}_test.tgz"

```

Or just:

```nohighlight
cp -- "${NAME}_test.tgz" /tmp

```

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/5_5575768a8748004e209b776fc1b2916d.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [September 8, 2010, 11:04am UTC](https://community.unix.com/t/question-on-using-a-variable-in-ksh/272993/3 "2010-09-08T11:04:29Z")

</div>

The problem is that underscore is a valid character in a variable name.  
The shell is substituting $NAME\_test not $NAME .  
Surrounding the variable name with braces is the solution and many scripters do this routinely.  
Following on from radolouv is is also conventional to put double quotes round strings whether or not it is mandatory.

```nohighlight
NAME="hello"
cp "${NAME}_test.tgz" "/tmp/${NAME}_test.tgz"

```

---

<div class="post-metadata">

**Author:** ![chatguy](https://community.unix.com/letter_avatar/chatguy/32/5_5575768a8748004e209b776fc1b2916d.png) [@chatguy](https://community.unix.com/u/chatguy)\
**Post date:** [September 8, 2010, 12:07pm UTC](https://community.unix.com/t/question-on-using-a-variable-in-ksh/272993/4 "2010-09-08T12:07:23Z")

</div>

Thanks so much...hm, for now on (no matter what--it looks cleaner anyway) I'll use double-quotation marks. Thanks so much for your help!
