LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2017, 10:13 AM   #1
bodisha
Member
 
Registered: Oct 2016
Posts: 31

Rep: Reputation: Disabled
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

Last edited by bodisha; 11-02-2017 at 10:14 AM.
 
Old 11-02-2017, 11:01 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,914

Rep: Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849
You might have to go back to the beginning of Unix to figure that one out. From what little I know it means substitution.

Code:
user=Fred
echo $user
When the shell runs the line "echo $user" Fred is substituted for the variable user and therefore when you run the script it outputs Fred.

I think what you mean by status of a command i.e.

Code:
output=$(ls -l *.log)
Is called command substitution i.e. the variable output is assigned the output of the ls command and not "ls -l *.log"
 
Old 11-02-2017, 11:42 AM   #3
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,617

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
I'm not sure that there is a "meaning" to the $..or even that it is a "special character" in this context. It is an element of the syntax used to define a variable. It's been used historically that way for a long time: BASIC alpha variables [$a, $b, etc] for example.
(How do I remember that? It's been > 40 years since I programmed in BASIC!)
 
Old 11-03-2017, 02:39 AM   #4
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 3,276
Blog Entries: 9

Rep: Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834
The $ character in Linux shells is a "substitution operator". It means "Treat this string as a variable name and substitute the current value". So when you are giving a command that relates to the name of the variable, you don't use $, but when you want the value, you do.
Code:
SAVE_DIR=~/saves
export SAVE_DIR
echo $SAVE_DIR
The export command doesn't use $ because it relates to the variable as such, telling the shell to store it in the environment. But the echo command uses $ because it is the value of the variable that you want to print out.
 
Old 11-04-2017, 08:53 AM   #5
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 3,911
Blog Entries: 1

Rep: Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828
$ export VAR="bubba"
$ echo $VAR

It can be used get the VALUE of a VARiable.

$ ls /usr/bin/ | grep -i z$

Or used in regex as an end of line and to find something that ends with z like above. And I think there's a special case for $$ as well, but don't recall what that is atm.
 
Old 11-05-2017, 01:38 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
To ask what "meaning" a character has depends on the context it's found in, because they can have different uses in different situations.

But in the shell, the "$" nearly always refers to a parameter expansion or substitution of some sort.

The most common use is for variable expansion. If you have a variable set, then placing "$" in front of the variable name substitutes the value of the variable, as shown in the several examples above.

Other common uses include process substitution [$(..)] and math substitution [$((..))].

It is also traditionally used as the default command prompt -- the first character on the line indicating where a command can be typed. You will see this in most shell coding examples on the net and elsewhere. Examples below:

Code:
$ wget "$( grep -Eo 'http://foo.com/[^"]+.jpg' <file.html )"
<wget output here>

$ echo "$(( 12 * 5 + 8 ))"
68
The first command searches "file.html" for all ".jpg" urls in it that start with "http://foo.com", and inserts them into the wget command, which then attempts to download them.


Other commands and programming languages, such as awk or perl, will often use the same characters, but in different ways. That makes learning how to properly use shell quoting very important when running them in the shell.


There are any number of beginning shell scripting tutorials on the net. Try out a few of them. The Bash Guide For Beginners is usually a good place to start:
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html

And finally, man bash is always the first place you should look when you have syntax questions. It's a long document, but well worth reading from end to end at least once (although I recommend first learning the scripting basics elsewhere before doing so).

Last edited by David the H.; 11-05-2017 at 01:46 AM. Reason: addendum
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
block special and character special files s_shenbaga Linux - Newbie 4 06-23-2015 02:16 AM
sed command to replace special character in special line rubylu Linux - Newbie 3 04-09-2015 01:45 PM
[SOLVED] Do special vi chars lose meaning if copied and pasted in script? Batistuta_g_2000 Linux - Newbie 8 02-20-2013 05:49 AM
Find and Replace character/special character from the file MyRelam Red Hat 8 05-21-2012 12:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration