# Defining variable problem

**URL:** <https://community.unix.com/t/defining-variable-problem/344461>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [July 14, 2014, 9:28pm UTC](https://community.unix.com/t/defining-variable-problem/344461 "2014-07-14T21:28:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![saleheen](https://community.unix.com/letter_avatar/saleheen/32/6_449c533f586939b6e5f246632a46421f.png) [@saleheen](https://community.unix.com/u/saleheen)\
**Post date:** [July 14, 2014, 9:28pm UTC](https://community.unix.com/t/defining-variable-problem/344461/1 "2014-07-14T21:28:12Z")

</div>

Hi

I'd say I'm having this weird problem where my script isn't taking the value off a variable or printing it. My code is like this:

```nohighlight
set count_C= `grep -c C mols`
set count_H= `grep -c H mols`
set count_O= `grep -c O mols`

sed -i '7,7 s/$/ $count_C $count_O $count_H/g' input 

```

It doesn't work. It prints $count\_C instead of the value. I've tried various ways, even echo $count\_C doesn't return my value. I tried

```nohighlight
count_C= $(grep -c C mols)

```

and also this:

```nohighlight
set count_C= $(grep -c C mols)

```

but with no result.

Strange thing is I tried the following in the terminal instead of my script and it worked,

```nohighlight
set count_C= `grep -c C mols`
echo $count_C

```

But the same thing doesn't work in my script.Instead of echoing, it only shows a blank space. In the end I'd like to know how I would use my variables in sed to insert that value in a specific line.  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [July 14, 2014, 10:09pm UTC](https://community.unix.com/t/defining-variable-problem/344461/2 "2014-07-14T22:09:04Z")

</div>

As has been said many times before on these forums, shell variables are not expanded inside single quotes. Try changing:

```nohighlight
sed -i '7,7 s/$/ $count_C $count_O $count_H/g' input

```

to:

```nohighlight
sed -i "7s/$/ $count_C $count_O $count_H/g" input

```

---

<div class="post-metadata">

**Author:** ![saleheen](https://community.unix.com/letter_avatar/saleheen/32/6_449c533f586939b6e5f246632a46421f.png) [@saleheen](https://community.unix.com/u/saleheen)\
**Post date:** [July 14, 2014, 10:21pm UTC](https://community.unix.com/t/defining-variable-problem/344461/3 "2014-07-14T22:21:20Z")

</div>

Thanks a lot Don!So I tried this as you said:

```nohighlight
#!/bin/bash
set count_C=`grep -c C mols`
set count_H=`grep -c H mols`
set count_O=`grep -c O mols`
sed -i "7s/$/ $count_C $count_O $count_H/g" input

```

still not working! ☹

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [July 14, 2014, 10:55pm UTC](https://community.unix.com/t/defining-variable-problem/344461/4 "2014-07-14T22:55:02Z")

</div>

> [@saleheen](#):
>
> Thanks a lot Don!So I tried this as you said:
> 
> ```plaintext
> #!/bin/bash
> set count_C=`grep -c C mols`
> set count_H=`grep -c H mols`
> set count_O=`grep -c O mols`
> sed -i "7s/$/ $count_C $count_O $count_H/g" input
> 
> ```
> 
> still not working! ☹

The `g` flag at the end of the substitute command shouldn't have any effect, but your three counts should have been added to the end of line 7 in the file named `input` .  
Saying it doesn't work, without showing us what it did do and without showing us what you were trying to do makes it very hard for us to figure out what went wrong. Give us a hint at what happened and what you wanted!

Show us the original contents of `input` .

Show us the contents of `input` after you ran your script.

Show us what you wanted `input` to look like after you run your script.

---

<div class="post-metadata">

**Author:** ![saleheen](https://community.unix.com/letter_avatar/saleheen/32/6_449c533f586939b6e5f246632a46421f.png) [@saleheen](https://community.unix.com/u/saleheen)\
**Post date:** [July 14, 2014, 11:05pm UTC](https://community.unix.com/t/defining-variable-problem/344461/5 "2014-07-14T23:05:14Z")

</div>

I'm sorry Don. Here's my "input"

> [@](#):
>
> Ruthenium 444 surface  
> 1.00000000000000  
> 10.8727200000000011 0.0000000000000000 0.0000000000000000  
> 0.0000000000000000 9.4160599999999999 0.0000000000000000  
> 0.0000000000000000 0.0000000000000000 21.5000000000000000  
> Ru  
> 64  
> Selective dynamics  
> Direct

My counts should return the values and I want add those values at the 7th line. This is what it should look like-

> [@](#):
>
> Ruthenium 444 surface  
> 1.00000000000000  
> 10.8727200000000011 0.0000000000000000 0.0000000000000000  
> 0.0000000000000000 9.4160599999999999 0.0000000000000000  
> 0.0000000000000000 0.0000000000000000 21.5000000000000000  
> Ru  
> 64 2 1 3  
> Selective dynamics  
> Direct

The numbers obviously can vary depending upon the count. The input file remains just the same as before after running the script. No change happens. 😕  
And also, here's the mols file from where it should count the values,

> [@](#):
>
> H 0.1823565363480026 0.4604101472535956 0.4005758974370104  
> H 0.2570433232476637 0.6231167698839984 0.3815017011047572  
> H 0.2135086777493862 0.5877276165535853 0.4600917787525729  
> C 0.2493254339588567 0.5395125405888599 0.4170749011908661  
> C 0.3737714706179839 0.4730560514301286 0.4295765297889743  
> O 0.4284154246482464 0.4351728266366453 0.3710697347999991

Thanks again!

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [July 14, 2014, 11:54pm UTC](https://community.unix.com/t/defining-variable-problem/344461/6 "2014-07-14T23:54:35Z")

</div>

```nohighlight
#!/bin/bash
count_C=$(grep -c C mols)
count_H=$(grep -c H mols)
count_O=$(grep -c O mols)
value="${count_C} ${count_O} ${count_H}"
awk -v value="$value" 'NR==7{$0=$0" "value}1' input

```

---

<div class="post-metadata">

**Author:** ![saleheen](https://community.unix.com/letter_avatar/saleheen/32/6_449c533f586939b6e5f246632a46421f.png) [@saleheen](https://community.unix.com/u/saleheen)\
**Post date:** [July 15, 2014, 12:06am UTC](https://community.unix.com/t/defining-variable-problem/344461/7 "2014-07-15T00:06:10Z")

</div>

Thanks a lot itkamaraj! It worked! Would you be kind enough the awk part? And how do I make it permanent? It's showing the changes in terminal but not in the file.

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [July 15, 2014, 12:15am UTC](https://community.unix.com/t/defining-variable-problem/344461/8 "2014-07-15T00:15:29Z")

</div>

```nohighlight
awk -v value="$value" 'NR==7{$0=$0" "value}1' input > /tmp/output
mv /tmp/output input

```

-v - define variable for awk  
value=$value - value is a variable name which holds the value of $value. For understanding purpose, make it as v=$value # v is the variable which holds the value of $value

NR = give the number of records being processed.  
NR==7 when awk reads the 7th line, we are changing the 7th line value as "7th line value + $value" and for printing all the lines i added 1 in the end. which is equivalent of {print}

---

<div class="post-metadata">

**Author:** ![saleheen](https://community.unix.com/letter_avatar/saleheen/32/6_449c533f586939b6e5f246632a46421f.png) [@saleheen](https://community.unix.com/u/saleheen)\
**Post date:** [July 15, 2014, 1:15am UTC](https://community.unix.com/t/defining-variable-problem/344461/9 "2014-07-15T01:15:10Z")

</div>

Awesome man! Thanks! 😃
