# function not see variable in script

**URL:** <https://community.unix.com/t/function-not-see-variable-in-script/273120>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [September 10, 2010, 8:42am UTC](https://community.unix.com/t/function-not-see-variable-in-script/273120 "2010-09-10T08:42:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShinTec](https://community.unix.com/user_avatar/community.unix.com/shintec/32/1793_2.png) [@ShinTec](https://community.unix.com/u/ShinTec)\
**Post date:** [September 10, 2010, 8:42am UTC](https://community.unix.com/t/function-not-see-variable-in-script/273120/1 "2010-09-10T08:42:57Z")

</div>

Hi Forum

Can anyone tell me whats wrong with my script. What i want to do read in values from a input file using a while loop then taking that input from the file into a function that i created. Every time i execute the script it goes through the while loop but the function doesn't see the values that im trying to pass it from the file.

Here is my code

```nohighlight
##################################
#this function validate the cost of a product
validCost()
{
  
  echo ${1} | grep -E '[:digit:]+.[:digit:]+'

  if [$? = 0]; then
  {
      echo 'this is valid'
      echo ${1}
  }
  else
  {
      echo 'fail hard out'
      echo ${1}
  }
  fi   
}

##################################
#Main body
##################################

#testing a while loop reading in file

echo '##################'
echo ${1}
echo '##################'
echo 'start of the loop'

echo '##################'
while read ${x} 
do
  
echo '##################'
  echo 'in the loop'
  validCost ${x}
echo '##################'
done < ${1}

echo 'end of loop'

```

############  
here my input file  
##############

```nohighlight
CHI132456
CHI132456
CHI132
1.5
2.6

```

i just want to know why the function cant see any data that i pass to it from the file.Any help would be much appreciated and thank you in advance

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_22734026db12803ebb3e059622ac3534.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 10, 2010, 8:49am UTC](https://community.unix.com/t/function-not-see-variable-in-script/273120/2 "2010-09-10T08:49:33Z")

</div>

Hi.

What shell are you using?

Try:

```nohighlight
while read x

```

instead of

```nohighlight
while read ${x} 

```

---

<div class="post-metadata">

**Author:** ![ShinTec](https://community.unix.com/user_avatar/community.unix.com/shintec/32/1793_2.png) [@ShinTec](https://community.unix.com/u/ShinTec)\
**Post date:** [September 10, 2010, 9:03pm UTC](https://community.unix.com/t/function-not-see-variable-in-script/273120/3 "2010-09-10T21:03:21Z")

</div>

im using the bash shell oh i give that a try

---------- Post updated at 11:03 AM ---------- Previous update was at 09:35 AM ----------

didnt work ☹  
keeps comming up with blanks

---

<div class="post-metadata">

**Author:** ![frank\_rizzo](https://community.unix.com/letter_avatar/frank_rizzo/32/6_22734026db12803ebb3e059622ac3534.png) [@frank\_rizzo](https://community.unix.com/u/frank_rizzo)\
**Post date:** [September 10, 2010, 9:10pm UTC](https://community.unix.com/t/function-not-see-variable-in-script/273120/4 "2010-09-10T21:10:04Z")

</div>

how are you running it? please poste entire output after adding set -x to the top.

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_22734026db12803ebb3e059622ac3534.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 10, 2010, 9:16pm UTC](https://community.unix.com/t/function-not-see-variable-in-script/273120/5 "2010-09-10T21:16:37Z")

</div>

Removing all the junk:

```nohighlight
validCost()
{
  
  echo $1 | grep -E '[0-9]+\.[0-9]+' > /dev/null

  if [$? -eq 0]; then
      echo 'this is valid'
      echo $1
  else
      echo 'fail hard out'
      echo $1
  fi   
}
##################################
#Main body
##################################

#testing a while loop reading in file

while read x
do
  validCost $x
done < ${1:-file1}

```

Output:

```nohighlight
fail hard out
CHI132456
fail hard out
CHI132456
fail hard out
CHI132
this is valid
1.5
this is valid
2.6

```

---

<div class="post-metadata">

**Author:** ![frank\_rizzo](https://community.unix.com/letter_avatar/frank_rizzo/32/6_22734026db12803ebb3e059622ac3534.png) [@frank\_rizzo](https://community.unix.com/u/frank_rizzo)\
**Post date:** [September 10, 2010, 9:26pm UTC](https://community.unix.com/t/function-not-see-variable-in-script/273120/6 "2010-09-10T21:26:16Z")

</div>

1) add a slash before the .  
2) not an issue but i suggest using -q option to grep instead of redirecting stdout to /dev/null

```nohighlight
echo ${1} | grep -q -E '[:digit:]+\.[:digit:]+'

```

---

<div class="post-metadata">

**Author:** ![ShinTec](https://community.unix.com/user_avatar/community.unix.com/shintec/32/1793_2.png) [@ShinTec](https://community.unix.com/u/ShinTec)\
**Post date:** [September 10, 2010, 9:37pm UTC](https://community.unix.com/t/function-not-see-variable-in-script/273120/7 "2010-09-10T21:37:14Z")

</div>

Oh i found the problem I was not give the full path name to the script  
i was using this

```nohighlight
q1 text.txt

```

when i should of been doing this

```nohighlight
q1 ~/text.text

```

Scottn does this line

```nohighlight
done < ${1:-file1}

```

tell the script to look in the current directory or am i way off

ps thank scottn/frank for the solution i really appreciate it 😃

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_22734026db12803ebb3e059622ac3534.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 10, 2010, 9:45pm UTC](https://community.unix.com/t/function-not-see-variable-in-script/273120/8 "2010-09-10T21:45:46Z")

</div>

Hi.

No. I used `${1:-file1}` just because I got too lazy typing the filename in every time I ran the script! It means, if no filename is given on the command line, use a file called file1 instead.

So, apart from the ${x} versus x thing, there was really nothing wrong with your script!

---

<div class="post-metadata">

**Author:** ![frank\_rizzo](https://community.unix.com/letter_avatar/frank_rizzo/32/6_22734026db12803ebb3e059622ac3534.png) [@frank\_rizzo](https://community.unix.com/u/frank_rizzo)\
**Post date:** [September 10, 2010, 9:50pm UTC](https://community.unix.com/t/function-not-see-variable-in-script/273120/9 "2010-09-10T21:50:24Z")

</div>

> [@shintec](#):
>
> O  
> Scottn does this line
> 
> ```plaintext
> done < ${1:-file1}
> 
> ```
> 
> tell the script to look in the current directory or am i way off

This reads "if no argument($1 not set or null) is passed use file1 as the default". If file1 is not in the current working directory then it will fail unless you pass it.

---------- Post updated at 20:50 ---------- Previous update was at 20:46 ----------

> [@scottn](#):
>
> So, apart from the ${x} versus x thing, there was really nothing wrong with your script!

actually there was a problem with the regex -- . was matching all characters. some how this was siliently fixed in your code :rolleyes:

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [September 10, 2010, 9:50pm UTC](https://community.unix.com/t/function-not-see-variable-in-script/273120/10 "2010-09-10T21:50:49Z")

</div>

Hi.

A minor note: for the type of character class that you wanted to use:

> [@shintec](#):
>
> ...echo ${1} | grep -E '[:digit:]+.[:digit:]+' ...

You need to use an extra set of brackets:

```plaintext
       Finally, certain named classes of characters are predefined within
       bracket expressions, as follows. Their names are self explanatory, and
       they are [:alnum:], [:alpha:], [:cntrl:], [:digit:], [:graph:],
       [:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:].
       For example, [[:alnum:]] means [0-9A-Za-z], except the latter form
       depends upon the C locale and the ASCII character encoding, whereas the
       former is independent of locale and character set. (Note that the
       brackets in these class names are part of the symbolic names, and must
       be included in addition to the brackets delimiting the bracket
       expression.)

-- excerpt from man grep, q.v.

```

cheers, drl

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_22734026db12803ebb3e059622ac3534.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 10, 2010, 9:52pm UTC](https://community.unix.com/t/function-not-see-variable-in-script/273120/11 "2010-09-10T21:52:56Z")

</div>

> [@frank\_rizzo](#):
>
> This reads "if no argument($1 not set or null) is passed use file1 as the default". If file1 is not in the current working directory then it will fail unless you pass it.
> 
> ---------- Post updated at 20:50 ---------- Previous update was at 20:46 ----------
> 
> actually there was a problem with the regex -- . was matching all characters. some how this was siliently fixed in your code :rolleyes:

Oh yes, the regex... 🙂 (not sure what you mean by silently)

But apart from that...
