Re: Should integer variables be quoted?



Lao Ming <laomingliu@xxxxxxxxx> writes:

I've often seen the refrain about shell variables
always being quoted (which I agree with)
but I've also found instances where they
cannot (and probably should not) be quoted.
An example of this is a case statement
or an if statement where an integer is
being compared. Doesn't quoting an integer
make it a string and thus break the compare?

You are confusing compiler quoting with shell quuoting.

When you quote somethign in a compiler, you tell the software that you
are providing a string. The shell doesn't work that way.

Think of shell "quoting" as a flag, per character, to tell the shell if it's
a special character (like '*' OR '?' OR '$') or a character that the
shell treats as a plain character.

So when you quote something, it tells the shell to consider the
characters as typed

Therefore
"a*b"
a"*"b
a\*b
'a*b'
'a''*''b'
\a\*\b
all do the same thing.

Use the echo command to experiment.

echo a*b

will list files that start with a and end with b, because the * is
considered a special character that the shell "globs" into a list of
files mathcing the pattern.

echo "a*b"
treats '*' as a plain character with no special properties (like a and b)

.



Relevant Pages

  • Re: shell scripting with solaris, help.
    ... Note that it's always preferable to separate options from other ... Bourne shell), so, if you want the script to be interpreted by a ... shell that recognizes the same syntax as the sh of other Unices, ... you want the first character of the first line of the ...
    (comp.unix.solaris)
  • comp.unix.shell FAQ - Answers to Frequently Asked Questions Part 2.
    ... character except '\0' is allowed in a file path on Unix. ... the default shell" isn't a good reason. ... command line buffer), and will fail if any file names contain a ... How do I use shell variables in awk scripts ...
    (comp.unix.shell)
  • Re: Excercices
    ... > example or get a better shell. ... program (in cmd.exe it is an internal command), ... In the above example there is neither a escape character nor ... You are called "Phil" so you can be distinguished from other ...
    (alt.lang.asm)
  • Re: (patch for Bash) toupper/tolower, isupper/islower, etc...
    ... >> Shell has problem in examining and operating on string, ... >> Now, to convert to uppercase or to lowercase, ... writing special case codes every time I wanted to something with those ... To change the first character of 'abc' to uppercase, ...
    (comp.unix.shell)
  • Re: exp_fexpectl does not work the same as expect shell, bug in C expect? Updated
    ... "echo $status" is not at the end of line but inside the string. ... character at a time just as the exp_fexpectl is, ... Could the shell be doing a forward read so that is ...
    (comp.lang.tcl)