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 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 The trailing Perl's choice to use (BTW, Awk doesn't use sigils. It does have a |
|||||
|
|
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. |
|||||||||
|
|
awk? 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:
(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 |
|||||
|
