# executing a variable assignment in a string

**URL:** <https://community.unix.com/t/executing-a-variable-assignment-in-a-string/266807>\
**Category:** Shell Programming and Scripting\
**Created:** [June 1, 2010, 8:07am UTC](https://community.unix.com/t/executing-a-variable-assignment-in-a-string/266807 "2010-06-01T08:07:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dexdyne](https://community.unix.com/letter_avatar/dexdyne/32/6_449c533f586939b6e5f246632a46421f.png) [@dexdyne](https://community.unix.com/u/dexdyne)\
**Post date:** [June 1, 2010, 8:07am UTC](https://community.unix.com/t/executing-a-variable-assignment-in-a-string/266807/1 "2010-06-01T08:07:57Z")

</div>

I'm using whatever the scripting is in this copy of busybox ! So not the full boxed set sometimes.

If I do

cmd="echo fred"  
$cmd

the system prints "fred"

if I do

cmd="fred=9"  
$cmd

it barfs.

Is there a simple way to put a varabble assignment in a string and execute it?

David

---

<div class="post-metadata">

**Author:** ![dennis.jacob](https://community.unix.com/user_avatar/community.unix.com/dennis.jacob/32/798_2.png) [@dennis.jacob](https://community.unix.com/u/dennis.jacob)\
**Post date:** [June 1, 2010, 8:10am UTC](https://community.unix.com/t/executing-a-variable-assignment-in-a-string/266807/2 "2010-06-01T08:10:47Z")

</div>

Try:

```nohighlight
cmd="fred=999"
eval $cmd
echo $fred

```

---

<div class="post-metadata">

**Author:** ![dexdyne](https://community.unix.com/letter_avatar/dexdyne/32/6_449c533f586939b6e5f246632a46421f.png) [@dexdyne](https://community.unix.com/u/dexdyne)\
**Post date:** [June 1, 2010, 8:20am UTC](https://community.unix.com/t/executing-a-variable-assignment-in-a-string/266807/3 "2010-06-01T08:20:07Z")

</div>

Thanks

I knew I'd seen it done somewhere.... just couldn't google how!

TVM
