# Variable Assignment

**URL:** <https://community.unix.com/t/variable-assignment/361461>\
**Category:** Shell Programming and Scripting\
**Created:** [July 17, 2016, 6:34pm UTC](https://community.unix.com/t/variable-assignment/361461 "2016-07-17T18:34:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankur328](https://community.unix.com/letter_avatar/ankur328/32/6_22734026db12803ebb3e059622ac3534.png) [@ankur328](https://community.unix.com/u/ankur328)\
**Post date:** [July 17, 2016, 6:34pm UTC](https://community.unix.com/t/variable-assignment/361461/1 "2016-07-17T18:34:19Z")

</div>

Hi

I am facing a problem.

```nohighlight
export local_folder=/opt/app/
cd /opt/app/abc/
abcversion="abc*" (abcga5 is inside /opt/app/abc/)
echo $abcversion (it echoes the correct version as abcga5 )

```

Now when I reuse the value of abcversion for a below path:

```nohighlight
export ABC_HOME=/opt/app/abc/$abcversion/

echo "$ABC_HOME"

```

It prints

```nohighlight
/opt/app/abc/abc*/

```

How can i get the actual value of abcversion in another variable assignment?

I tried searching the net and various tweaks but didnt work.

Thanks

---

<div class="post-metadata">

**Author:** ![Ivo\_Breeden](https://community.unix.com/letter_avatar/ivo_breeden/32/6_22734026db12803ebb3e059622ac3534.png) [@Ivo\_Breeden](https://community.unix.com/u/Ivo_Breeden)\
**Post date:** [July 17, 2016, 7:04pm UTC](https://community.unix.com/t/variable-assignment/361461/2 "2016-07-17T19:04:11Z")

</div>

When you assign

```nohighlight
abcversion="abc*"

```

then $abcversion will contain just "abc\*". It will not be set to "abcga5" because the quotes prevent globbing.  
In the next line you do:

```nohighlight
 echo $abcversion

```

which is expanded by the shell to  
echo abc\* and only then the abc\* is expanded to abcga5.

If you do want the variable "abcversion" have the value of abcga5 you should omit the quotes in the assignment:

```nohighlight
abcversion=abc*

```

---

<div class="post-metadata">

**Author:** ![ankur328](https://community.unix.com/letter_avatar/ankur328/32/6_22734026db12803ebb3e059622ac3534.png) [@ankur328](https://community.unix.com/u/ankur328)\
**Post date:** [July 17, 2016, 9:33pm UTC](https://community.unix.com/t/variable-assignment/361461/3 "2016-07-17T21:33:34Z")

</div>

I tried removing the quotes and here are the results:

```nohighlight
export local_folder=/opt/app/
cd /opt/app/abc/
abcversion=abc* (abcga5 is inside /opt/app/abc/)
echo $abcversion (it echoes the correct version as abcga5 )

```

```nohighlight
export ABC_HOME=/opt/app/abc/$abcversion/

echo "$ABC_HOME"

/opt/app/abc/abc*/

```

but I need this:

```nohighlight
/opt/app/abc/abcga5

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [July 18, 2016, 12:38am UTC](https://community.unix.com/t/variable-assignment/361461/4 "2016-07-18T00:38:27Z")

</div>

The double quotes prevent wildcard expansion, but not in the assignment ( `var=pattern*` is the same as `var="pattern*"` ),

but they do make a difference when used with the echo statement. To get the wildcard \* to match, they should be left out:

```nohighlight
echo $ABC_HOME

```

However, if there are multiple matches then you would get this:

```nohighlight
echo $ABC_HOME
/opt/app/abc/abcga5 /opt/app/abc/abcga6 /opt/app/abc/abcga7

```

---

<div class="post-metadata">

**Author:** ![ankur328](https://community.unix.com/letter_avatar/ankur328/32/6_22734026db12803ebb3e059622ac3534.png) [@ankur328](https://community.unix.com/u/ankur328)\
**Post date:** [July 18, 2016, 1:38am UTC](https://community.unix.com/t/variable-assignment/361461/5 "2016-07-18T01:38:51Z")

</div>

Hi

Its still not working. i removed quotes from everything:

See below:

```nohighlight
cd /opt/app/abc/
abcversion=abc* (abcga5 is inside /opt/app/abc/)
echo $abcversion
 

```

echoresult:

```nohighlight
abcga5

```

```nohighlight
export ABC_HOME=/opt/app/abc/$abcversion/

echo $ABC_HOME

```

echoresult:

```nohighlight
/opt/app/abc/abc*/

```

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [July 18, 2016, 2:59am UTC](https://community.unix.com/t/variable-assignment/361461/6 "2016-07-18T02:59:33Z")

</div>

Remove the trailing /  
Otherwise only matching directories are displayed.

---

<div class="post-metadata">

**Author:** ![Ivo\_Breeden](https://community.unix.com/letter_avatar/ivo_breeden/32/6_22734026db12803ebb3e059622ac3534.png) [@Ivo\_Breeden](https://community.unix.com/u/Ivo_Breeden)\
**Post date:** [July 18, 2016, 5:08pm UTC](https://community.unix.com/t/variable-assignment/361461/7 "2016-07-18T17:08:57Z")

</div>

Scrutinizer is right when he says: "the assignment ( `var=pattern*` is the same as `var="pattern*"` )". It can be tested by adding quotes around the argument of echo:

```nohighlight
abcversion=abc*
echo "$abcversion"

abc*

```

To activate wildcard expansion you should use an assignment like this:

```nohighlight
abcversion=$(ls abc*)
echo "$abcversion"

abcga5

```

And also MadeInGermany is right when he says to remove the trailing slash in  
`export ABC_HOME=/opt/app/abc/$abcversion/` (unless abcga5 is a directory).

Note: think about what the script should do when more files match the wildcard. Test it.
