0

I was surprised to get the following output from my csh code:

set myname = '*'
echo $myname

This prints me a list of all the names of my scripts in the folder from which I am executing my bash script:

myprogm1.py myprogrm2.py …

I wanted to hand over a simple string to myname: as star symbol '*'

How can I prevent my script from printing a list of names and just interpret the * as a string?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
firefly2517
  • 115
  • 2
  • 15
  • 1
    Have you tried escaping it with a backslash, as `\*`? – Andras Deak -- Слава Україні Jul 31 '15 at 00:05
  • Enclose the variable in double quotes in the `echo "$name"` statement. Almost always close the variable in double quotes. Omit the double quotes only when you know enough to know when it is correct to omit them. Note that the assignment works precisely as you wanted it to (because you used quotes — single quotes are fine in this case because there was no variable to expand). It is the processing for the `echo` command that expands the `*` into a list of file names. – Jonathan Leffler Jul 31 '15 at 00:17
  • Thank you @jonathanleffler, I am new in the bash business and need to learn a lot or should I say '*' ... – firefly2517 Jul 31 '15 at 00:40
  • There are strong grounds for preferring [`"$@"`](http://stackoverflow.com/questions/255898/how-to-iterate-over-arguments-in-bash-script/256225#256225) over `$*` most of the time…but that's a jocular aside rather than a serious critique of anything directly relevant to this question (or, strictly, your comment). – Jonathan Leffler Jul 31 '15 at 00:43

2 Answers2

3

Actually, your variable properly gets the asterisk as value, due to the single quotes. However, when you call echo the asterisk is expanded as a wildcard and matches every file. To avoid it, simply put your variable between double quotes:

set myname='*'
echo "$myname"

(Note that there are a lot of differences between csh and bash syntax.)

0

Word of the day: "metacharacters". Find the metacharacters for your shell... they differ hugely between Borne and C shells.

Shells expand lines each time the line executes. Thus while myname does indeed contain the desired asterisk, when it is expanded without being quoted the shell sees an isolated asterisk and expands it.

If you make a regular practice of ruthlessly quoting variables in command lines you can avoid all sorts of issues.

Another "problem character" is space, especially if in file names. If not quoted each space causes a word break on expansion. Very bad in commands like

  set a = "hello world"
  set b = "foo bar"
  cp "$a" $b;       # becomes cp "hello world" "foo" "bar"
  rm $a;            # becomes rm "foo" "bar"
Gilbert
  • 3,740
  • 17
  • 19
  • I see what you're trying to say, but your comments don't explain it sufficiently well. To the confused eye, it looks like "that's what I said — and meant". Whereas you clearly know the difference. Maybe `cp ./hello ./world ./foo ./bar` and `rm ./foo ./bar` as the explanation, though you'd need to explain why the `./` are there — maybe 'becomes' should be 'equivalent to'. – Jonathan Leffler Jul 31 '15 at 00:26