3

I understand the usage single quote and double quote.

but I don't know situation need to double quotes in the script.

there is no diff that statements

$ echo hello world! $1
$ echo "hello world! $1"

please show me diff between normal and double quotes.

alijandro
  • 11,627
  • 2
  • 58
  • 74
  • Word splitting in bash is controlled by the `IFS` variable ("*internal field separator*") which defaults to `IFS=$' \t\n'` (`space`, `tab`, `newline`). If you do not quote your variables *word splitting* occurs on any character in `IFS` (you can set `IFS` to control this). Quoting will effect *file/path name expansion* as well. – David C. Rankin Aug 04 '16 at 03:24

2 Answers2

5

Let's consider a directory with these files:

$ ls foo*
foo111.txt  foo11.txt  foo1.txt

Let's consider a minor variation on your script:

$ cat script
#!/bin/sh
echo No quotes $1
echo "Double quotes $1"

Now, let's run it:

$ bash script "foo*"
No quotes foo111.txt foo11.txt foo1.txt
Double quotes foo*

As you can see, the results are completely different. Without the double quotes, pathname expansion is performed.

To illustrate another difference:

$ bash script "long              space"
No quotes long space
Double quotes long              space

With double quotes, the long space between words is preserved. Without it, all contiguous whitespace is replaced with a single blank. This is an example of word splitting.

John1024
  • 109,961
  • 14
  • 137
  • 171
1

An example might demonstrate the use

  1. To accommodate string with spaces

    var=file name # Not the intended effect.
    

    file is stored in a var and name is taken by shell as a separate cmd which gives you an error.

  2. To prevent word splitting

    var="file name"
    cp $var newfile
    

    Here $var expands to file name and in effect, the command would become

    cp file name newfile
    

    and cp would take file and name as 2 source files and newfile as the destination directory which gives you the error:

    cp: target 'newfile' is not a directory
    

    If there really exists a directory named 'newfile', it will give error:

    cp: cannot stat 'file': No such file or directory
    cp: cannot stat 'name': No such file or directory
    

    The correct method is

    cp "$var" newfile
    

    In this case, the fully expanded $var is considered a single string.

anishsane
  • 20,270
  • 5
  • 40
  • 73
sjsam
  • 21,411
  • 5
  • 55
  • 102
  • @anishsane : Appreciate the edit.. – sjsam Aug 04 '16 at 05:19
  • 1
    I notice you have answered many [tag:bash] questions which have then been closed as duplicates. Perhaps you'll want to review the [Stack Overflow `bash` tag wiki](http://stackoverflow.com/tags/bash/info) for a list of frequently asked questions; obviously, feel free to help us out with keeping it current, understandable, and organized Thanks in advance! – tripleee Aug 04 '16 at 06:09
  • 1
    @tripleee : I will take care of this in future. Was a bit lazy for this one I admit.. :) – sjsam Aug 04 '16 at 06:35