LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do I escape $# in shell scripting? (https://www.linuxquestions.org/questions/programming-9/how-do-i-escape-%24-in-shell-scripting-67655/)

KDE4me 06-24-2003 04:13 AM

How do I escape $# in shell scripting?
 
Hi,
I've tried to search for this on google and this site's search engine but they all can't recognize these characters so ...

I'm trying to test for how many parameters a command line argument is given and I use

if [ $# -eq 1 ]
then ...

anyway when I do this in vi & bash they don't recognize everything after #. Is there some way to escape this?

Thanks.

jharris 06-24-2003 04:51 AM

The following works for me
Code:

#!/bin/sh
if [ $# == 1 ]; then
  echo "Met condition"
fi

Code:

[jh@e128 jh]$ ./test.sh
[jh@e128 jh]$ ./test.sh bla
Met condition

HTH

Jamie...

apeekaboo 06-25-2003 05:26 PM

Don't know why it doesn't work. It works for me...
Are you sure the $# is the problem?
Usually I always quote the variables in my tests, even though it's not always necessary.
I just saves me some hassle whenever it does matter...
Code:

if [ "$#" -eq 1 ]; then
        echo "you gave 1 argument"
fi

You could test your script by executing it with 'bash -x ./scriptname' to see what really happens.

turnip 06-25-2003 10:35 PM

vi will see the # as a comment. It will still run as a shell script. I see this all the time in my scripts editing them in vi


All times are GMT -5. The time now is 02:05 PM.