Originally Posted by
John_Patrick_Mason
OK, now what's the deal with the operators? I was taught that the operators were + - / *.
They are using the term 'operator' to refer to the various tests you're performing, such as
In that example, the operator is -e
Originally Posted by
John_Patrick_Mason
I still don't get why quotes are necessary.
That's because that's not the best way to introduce the concept of quotes. Let me try to do better.
You know how you separate commands arguments with spaces, like so?
Code:
less -R somefile.txt
Ok, now what if instead of somefile.txt, the file we wanted to view was named Foo Foo Foo.txt? If you try to 'just use' that filename, this is what happens -
Code:
$ less -R Foo Foo Foo.txt
Foo: No such file or directory
Foo.txt: No such file or directory
Yeah yeah, we know those don't exist, not helpful. So how do we tell the shell to treat the filename as a single argument?
Well, there are several ways to solve this, but the simplest one is this -
Code:
less -R "Foo Foo Foo.txt"
Woo-Hoo, It Works!
So, to sum up what's said so far, use quotes when you want to limit what the shell parses. I believe that double-quotes, as we are using here, limit parsing to stuff involving the $ symbol, commands enclosed in backticks, and some backslash escape sequences. (I may have left something out, please correct me if so.)
Now, going back to the explanation you quoted -
"First, notice how the parameter $FILE is quoted within the expressions. This is not required, but it is a defense against the parameter being empty. If the parameter expansion of $FILE were to result in an empty value, it would cause an error (the operators would be interpreted as non-null strings rather than operators). Using the quotes around the parameter ensures that the operator is always followed by a string, even if the string is empty."
Variables in shell scripts are not like other languages. Their values just kinda get plopped straight in the code.
Example -
Code:
$ Foo="Bananas Monkey"
$ cat $Foo
cat: Bananas: No such file or directory
cat: Monkey: No such file or directory
This expanded to cat Bananas Monkey, which failed because I don't have files named Bananas or Monkey in that directory.
And again, with quotes -
Code:
$ cat "$Foo"
cat: 'Bananas Monkey': No such file or directory
This time it expanded to
Code:
cat "Bananas Monkey"
... which failed because I don't have a file with that name either.
Now what if Foo has no value?
The shell interprets this as simply cat, which will hang. (If you actually ran this, use Ctrl-C to get your shell back.)
Let's try it again, but with quotes -
Code:
$ cat "$Foo"
cat: '': No such file or directory
Notice that it expanded to cat "". See the difference? The shell treats the quotes' contents as a single unit, so it sees it as an argument to pass to cat, albeit an empty argument.
Armed with that, read this text again -
"First, notice how the parameter $FILE is quoted within the expressions. This is not required, but it is a defense against the parameter being empty. If the parameter expansion of $FILE were to result in an empty value, it would cause an error (the operators would be interpreted as non-null strings rather than operators). Using the quotes around the parameter ensures that the operator is always followed by a string, even if the string is empty."
Hope this clears that one up for you.
Bookmarks