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.
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
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.
Sometimes called a sigil, generally used for translating the names of variables (and others) into values.
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.)
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.