What is the meaning the $ special character?

Hello... and thank you in advanced for any help anyone can offer me

I'm hoping someone can explain what the leading $ is/means (i.e. $PS1, $HOME, etc).... I was having a discussion with someone and was trying to explain it... Which I felt like I came up kind of short with how well I did it.

I understand it's a special character and how to use it if I want to see the value of a variable or if I want to see the status of a command... I'm just unsure what kind of special character its categorized as or the definition of it's exact function.

I got home and googled it... I found plenty of explanations on how to use it but didn't find an adequate explanation of what it is and it's definition. It seems like every special character is well documented except the $... Could someone explain to me how it's categorized and it's extract definition?

Once again... thanks for reading this and any help anyone can offer

Hi see here: The Open Group Base Specifications Issue 7, 2016 Edition

Note:
See also XCU Parameters and Variables.

--
"Parameter expansion" is the way to get the value that is stored in a parameter ( for example a variable )

So for example if you have a variable foo and you give it the value bar

foo=bar

, then the variable foo contains the value bar .
Then, to get the content of the variable foo , you prepend a $ sign:

echo "$foo"
bar

Bodisha,

The best way I like to remember it is that it is a marker to show a variable.

for instance this is my PS1

HOST=`hostname`
PS1='$LOGNAME@$HOST: $PWD>

PS1 = (the following)
$LOGNAME is the variable LOGNAME that holds my login name
$HOST is the variable that holds the hostname of the server
$PWD is the variable that holds the current (print) working directory

so if I want to echo this PS1 out to see what it looks like I would use:

dk@server: /home/dk> echo $PS1
$LOGNAME@$HOST: $PWD>
dk@server: /home/dk>

hope this helps

Also note that there are some special variables which are also introduced by "$":

$$ = process number of the currently running process
$* = all the arguments passed to a process (or subfunction of a script)
$@ = same as above*)
$# = number of all the arguments passed to a process (or subfunction of a script)
$! = process number of the background process invoked by the current process
$? = the return code of the last process executed
$1 ($2, $3, ...) = the first (second, third, ...) positional parameter passed to that process

I hope this helps.

bakunin
____________
) "$" and "$@" are basically the same, except when quoted. "$*" gives all arguments surrounded by spaces, "$@" gives all arguments surrounded by quotes. For instance, you call a script this way:

myscript.sh "one arg" "two arg" "three arg"

Inside the script you use "$*" and you get a single string: "one arg two arg three arg", whereas when you use "$@" you get three strings: "one arg" "two arg" "three arg".

1 Like

$() Command substitution, similar to `` (two back quotes) but without some of the same problems.
e.g.

now=$(date)

Same page that Scrutinizer shared in post #2 but a bit afterward

Hi.

Extensive use in software. See Dollar sign - Wikipedia

Also in development situations, build utility make is often used, and some variables of interest there are:

Automatic Variables that make will set after a rule match:

   $@  Filename representing the target
   $%  Filename element of archive member specification
   $<  Filename of the first prerequisite
   $?  Names of all prerequisites newer than target, space separated
   $^  Names of all prerequisites, duplicates removed, spaced
   $+  Same as $^, but with duplicates removed, spaced
   $*  Stem of the target - typically file without suffix

   from: GNU make, 3rd, O'Reilly, pages 16-17

As is often the case, fact-based questions are often best answered by searching, Google and Wikipedia are your friends.

Best wishes ... cheers, drl

2 Likes

Hi, Aia.

Yes, I noted that. His title was What is the meaning the $ special character?

He also asked ...explain to me how it's categorized and it's extract [sic] definition?

He did not restrict the meaning to any special instance.

I appreciate that he used Google, but I think he may not have had enough time to internalize the information. I know that I don't always understand something just after I read it.

For example from the Wikipedia article: In most shell scripting languages, $ is used for interpolating environment variables, special variables ... -- it seems hard to imagine an alternate definition.

Thanks for the comment, it's always useful to have feedback and more than one set of dispassionate eyes looking over things ... cheers, drl

1 Like

Indeed, I was looking at that on the wiki page and reread it several times and still do not know what it means.

Full quote:

Hi, Scrutinizer.

I think we know what most of that means, such as in:

$ echo $a $SHLVL $(( 2+2 ))
hi 1 4

which also illustrates the use of $ as part of the prompt.

Do we agree on that much, or have I misunderstood you? ... cheers, drl

Hi drl, yes of course we know what they meant to say, but not thanks to that wiki content.

I was just trying understand the words "interpolating" and "performing translation of localised strings" in that sentence, which I think are either wrong, obfuscating or needlessly complicated and most certainly will make someone new to the subject glaze over..

1 Like

The word interpolate is one of many English words with several meanings. I certainly agree that the mathematical meaning of interpolate ("insert an intermediate value or term into a series by estimating or calculating it from known surrounding values") makes no sense in this context. And this is how interpolate is most frequently used in this forum.

But, one of the other meanings of interpolate ("alter (a book or text) by insertion of new material") fits what parameter expansion, arithmetic expansion, command substitution, and special variable expansion (each of which are introduced by a leading $ in the shell command language) do if you consider shell commands to be text.

Hi, Don.

I searched for interpolate in the forum, and got 56 hits. Looking at a sample of 10 (trying to be objective), I found 9 referred to variables, 1 to the math ... cheers, drl

---------- Post updated at 09:01 ---------- Previous update was at 08:43 ----------

Hi.

Apologies for the long post. Here is a sample of results of a Google search:

Mon Nov  6 05:36:09 CST 2017

The "$" as dereference, interpolation, expansion operator.

-----

Google search:
compare interpolate dereference expand
( I added "-perl", got 143K hits )

-----

In Unix shell scripting and in utilities such as Makefiles, the
dollar sign "$" is the dereference operator, used to translate
the name of a variable into its contents, and is notably absent
when assigning to a variable.

In various languages, prefixes are used in identifiers, known as
sigils. These are not unary operators - syntactically they are
lexically part of the identifier, and have different semantics,
such as indicating the data type of the identifier - but are
syntactically similar to the dereference operator and can be
confused with it. For example, in a shell script $FOO is the
dereference operator $ applied to the variable FOO, while in Perl
$foo is a scalar variable called foo. 

https://en.wikipedia.org/wiki/Dereference_operator

-----

In computer programming, string interpolation (or variable
interpolation, variable substitution, or variable expansion) is
the process of evaluating a string literal containing one or more
placeholders, yielding a result in which the placeholders are
replaced with their corresponding values. It is a form of simple
template processing[1] or, in formal terms, a form of
quasi-quotation (or logic substitution interpretation). String
interpolation allows easier and more intuitive string formatting
and content-specification compared with string concatenation.[2]

https://en.wikipedia.org/wiki/String_interpolation

-----

Strings with single quotes (') are literal strings, meaning there
are no special characters; every character in the string will
output as itself. Strings with double quotes (") in powershell
will expand variables and escape characters inside the string;
this is referred to as interpolation.

http://www.powershellish.com/blog/2014-12-09-strings-expansion

-----

The second aspect of shell variable syntax worth noting is the
use of the dollar sign when referring to a variable ... use
the dollar sign to get the value of the variable.

bash Cookbook: Solutions and Examples for bash Users
By Carl Albing, JP Vossen, Cameron Newham

-----

bash derives much of its programming functionality from shell
variables. We've already seen the basics of variables. To recap
briefly: they are named places to store data, usually in the form
of character strings, and their values can be obtained by
preceding their names with dollar signs ($). Certain variables,
called environment variables, are conventionally named in all
capital letters, and their values are made known (with the export
statement) to subprocesses.

If you are a programmer, you already know that just about every
major programming language uses variables in some way; in fact,
an important way of characterizing differences between languages
is comparing their facilities for variables.

The chief difference between bash's variable schema and those of
conventional languages is that bash's places heavy emphasis on
character strings. (Thus it has more in common with a
special-purpose language like SNOBOL than a general-purpose one
like Pascal.) 

https://www.safaribooksonline.com/library/view/learning-the-bash/1565923472/ch04s02.html

From my perspective, there is a lot of information about the use of a $, and if the OP was referring to its use in bash , there are many places that explain it, as well as in many books.

I tend to think of the variables as a location in memory that has the string that is the name of variable as its content. Along with that, there is a link (pointer, etc., method unspecified) that allows us to view and change that value, and we can see that value with a $.

My recollection is that when this level of detail came up in class, I told the students my visualization. I think that usually satisfied the more technical, curious students.

Best wishes ... cheers, drl