6

This question already has an answer here:

In Bash, when I do:

foo="*"
echo $foo

It expands * to the contents of the current folder. How do I make sure it just prints a literal *?

The same, by the way, happens with a regular echo "$foo", it prints the contents of the current folder.

marked as duplicate by Gilles bash Apr 20 '15 at 22:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Thanks @don_crissti: It works, but it prints the contents of the current folder. – Amelio Vazquez-Reina Apr 20 '15 at 22:23
  • @don_crissti My wrong! Sorry I read that too quickly. Can't believe I fell victim to this. – Amelio Vazquez-Reina Apr 20 '15 at 22:29
12

Let us define foo:

$ foo="*"

Now, try echo without quotes:

$ echo $foo
File1 File2

The replacement of * with a list of filenames is called pathname expansion. It can be suppressed with with double-quotes:

$ echo "$foo"
*

In addition, double-quotes will prevent brace expansion, tilde expansion, and word splitting.

For completeness, try echo with single quotes:

$ echo '$foo'
$foo

Single quotes prevent the shell from making any substitutions at all.

  • 1
    Of course! I can't believe I missed this. I kept thinking of quoting the original assignment, not the expansion on the echo statement itself! – Amelio Vazquez-Reina Apr 20 '15 at 22:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.