A lot of scripting languages like Perl, Awk, Bash, PHP, etc. use a $ sign before identifier names. I've tried to look up why but never had a satisfactory answer.
10Possibly because of the last step in the business plans of most programmers - ... profit!– OdedNov 4, 2011 at 20:26
2Interesting post on SO regarding the same question: Why do Perl variables need to start with $, %, @ (sigils)– Brad ChristieNov 4, 2011 at 20:43
The Bourne shell or its predecessor was probably the origin of the convention. Shells tend to use
$foo to refer to a shell variable or environment variable, whereas just
foo is taken as a literal string (which could be a command name, a file name, or just a word to be passed to another command).
It's probably a matter of minimizing how much you have to type. In most programming languages, most of what you type is going to be keywords (from a small fixed set) and names of things that have been declared; it makes sense to use just a name for those. String literals are enclosed in quotation marks (single or double) to distinguish them syntactically.
In an interactive shell, most of the things you type are going to be command names and file names, so having an undecorated form for them makes sense. Variable names aren't used quite as often, so preceding them with
$ is a straightforward way to distinguish them.
$ that some BASICs use for string variables (as opposed to numeric variables) was probably an inspiration as well.
Perl's choice to use
$ for scalar variables was undoubtedly inspired by shells such as the Bourne shell, and early versions of Perl commonly treated barewords as string literals (
print FOO and
print "FOO" were equivalent). This use of barewords has been deprecated in more modern versions of Perl, but the sigils remain, and are mostly used to distinguish different kinds of variables. Perhaps if Perl were being designed from scratch, scalar variables might be denoted by an "empty sigil", but that didn't happen.
(BTW, Awk doesn't use sigils. It does have a
$ syntax, but it refers to field numbers.
NF is the number of fields;
$NF is the last (the
3Just a sidenote: Bourne shell (1977), BASIC (1964). Nov 6, 2011 at 12:52
A $ sign in variable name is a special case of "Sigil", Back in the early days of BASIC (1964 or so) $ was used to denote string variable names. My guess is, since it makes it easier on compilers to separate variable names from the rest of the grammar using some symbol, and since it was common to BASIC, its usage has progressed. However, this is not the only one used.
$for strings (and
%for integers) in Basic was a suffix rather than a prefix, at least in the (non-standard) variants I was inflicted with. Not that that's significant, of course - suffix vs. prefix is pretty superficial, at least as far as compilers and lexical analysis are concerned. There may be some slight difference in terms of readability.– user8709Nov 4, 2011 at 21:45
BTW - one reason it worked in Basic is that there was a very limited range of types, and no user-defined types, so only a very limited range of sigils was needed. The Perl usage is different - though it's been a long while now for me, so I can't really remember details.– user8709Nov 4, 2011 at 21:58
@steve314, you are absolutely correct A$=... Thanks for noting this.– NoChanceNov 4, 2011 at 22:01
This appears to originate in BASIC, where the $ suffix indicates a string variable, and was also pronounced as "string". For example, INKEY$ is pronounced "in key string".
But why the dollar sign? Well, $ looks a bit like S for string. And according to Wikipedia:
As the dollar sign is one of the few symbols that is, on one hand, almost universally present in computer character sets, but, on the other hand, rarely needed in their literal meaning within computer software, the $ character has been used on computers for many purposes unrelated to money.
(Appropriately, For a Few Dollars More is on the telly as I type this.)
Sometimes called a sigil, generally used for translating the names of variables (and others) into values.
Why not? It's easier to write a lexer that can tell an identifier is going to be a variable (and consequently easier to write the parser), especially for an interpreted language. As to why
$, that's probably largely tradition - one of the creators of an early language using the construct (Bash? or more likely a predecessor to it) used it for whatever reason and subsequent languages adopted it.
1It may not make the lexer and parser that much easier to write, but it does eliminate the requirement for reserved words.– TomGNov 5, 2011 at 0:47