[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: variables



Hi,

Hans wrote:
> variables (and strings) are set in different marks and what it does mean.
> I found "foo", `foo` and 'foo' at different tools. 

In manual pages you can find `text' or ``text''. There seem to be
some important religious rules about proper quotation marks at various
occasions.


In the context of the shell command line, " and ' both keep together
text snippets as single words, even if they contain characters which
normally would separate words (aka whitespace).

Here echo sees two words "fo" and "o" and prints them with a single
blank character inbetween:
  $ echo fo   o
  fo o

Here it sees only one word "fo   o":
  $ echo "fo   o"
  fo   o
  $ echo 'fo   o'
  fo   o

The difference comes with variable evaluation
  $ fo=FUN
  $ echo "$fo   o"
  FUN   o
  $ echo '$fo   o'
  $fo   o

Text inside single quotes ' is always kept as is.
Text inside double quotes " is further processed at $, `,  \, or !.

Another reason to have two kinds of quotation marks is to let one
make the other literal:
  $ echo '"'
  "
  $ echo "'"foo"'"
  'foo'

Character ` is not really a quotation mark but one of two ways
to require command substitution.

  $ x=`expr 1 + 1`
  $ echo "$x"
  2

The other form is more versatile because it can be nested.
This example counts the files in the current directory and adds 10:
  $ x="$(expr $(ls | wc -l) + 10)"
  $ echo "$x"
  132


Have a nice day :)

Thomas


Reply to: