Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-24-2003, 05:13 AM   #1
Registered: Apr 2003
Location: Australia
Distribution: RedHat 9.0 / Slackware 9.0/ FreeBSD 4.8 / Solaris 8 x86 / Mandrake 9.0
Posts: 90

Rep: Reputation: 15
How do I escape $# in shell scripting?

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?

Old 06-24-2003, 05:51 AM   #2
Senior Member
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
The following works for me
if [ $# == 1 ]; then
  echo "Met condition"
[jh@e128 jh]$ ./
[jh@e128 jh]$ ./ bla
Met condition

Old 06-25-2003, 06:26 PM   #3
Registered: Apr 2003
Location: Stockholm/Sweden
Distribution: Kubuntu, Debian, Slax
Posts: 91

Rep: Reputation: 16
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...
if [ "$#" -eq 1 ]; then
        echo "you gave 1 argument"
You could test your script by executing it with 'bash -x ./scriptname' to see what really happens.
Old 06-25-2003, 11:35 PM   #4
Registered: Jul 2002
Posts: 143

Rep: Reputation: 15
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 04:32 PM
SuidPerl Mail Shell Escape Vulnerability jeremy Linux - Security 1 04-27-2005 04:01 AM
bach scripting and escape characters... Bud-froggy Linux - Newbie 3 07-09-2004 05:55 PM
Escape character not working in shell script philipz Programming 1 04-29-2004 10:58 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:11 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration