LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-09-2010, 01:39 AM   #1
bala1486
LQ Newbie
 
Registered: Mar 2010
Posts: 7

Rep: Reputation: 0
Bash scripting doubt


Hello,
I have a basic doubt in bash scripting.
I read that "$" symbol is used for expansion. Suppose $var appears in a statement it just substitutes the value of var in that statement.

var=My name is Bala
This is wrong. It needs double quotes as it contains spaces.
right one is
var="My name is Bala"

Suppose
var="My name is Bala"
con=$var
I thought that the second line is wrong and it should be con="$var".
But bash prints My name is Bala if echo $con is typed. How is this possible?
The line should have expanded as con=My name is Bala if just substitution is used. What am i misunderstanding in this?

Also command like var=$(ls | grep data) also works the same as var="$(ls | grep data)" even if the result has more than 1 word.

Please explain me.. Thank you..

Thanks,
Bala
 
Old 04-09-2010, 01:58 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Regards var="My name is Bala", That works, so would var='My name is Bala'. Everything is single quotes is taken completely verbatim whereas expansion is done in double quotes. When bash finds something in single or double quotes it expands it to a single word.

Bash expands whatever is to the right of an assignment "=" as a single word. The GNU Bash Reference says "name=[value] ... All values undergo tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal ... Word splitting is not performed"
 
Old 04-09-2010, 02:15 AM   #3
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
Just to clarify for you further, var=$(ls | grep data) works because the expression is expanded, and var="$(ls | grep data)" works because it was treated as a single word, and then expanded. This expression var='$(ls | grep data)' would not work. The text inside the single quotes is not expanded and instead is taken as is. So $var would hold the text $(ls | grep data).
 
Old 04-09-2010, 04:08 AM   #4
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
Perhaps another point that is confusing you is that when a variable appears on the right and is expanded, it is treated as if you had placed quotes around it. That is why con=$var works, and var=My name is Bala doesn't.
 
Old 04-09-2010, 05:49 AM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
The key point is how the thing is parsed.

When you do this:

Code:
var=value with spaces
What your shell really understands is this:

Code:
var=value command parameters
This syntax is often used when running commands in linux, for example, a common use is to launch X programs on an alternate display:

Code:
DISPLAY=:0.1 gvim ~/.conkyrc
When you put the thing into a variable, the shell only sees a single token, and only after that it's expanded. So, the following would put the whole string into $DISPLAY but would not work as intended from the command line:

Code:
$ var=":0.1 gvim ~/.conkyrc"
$ DISPLAY=$var
# This obviously will not launch the command
# But instead set the var with the given value
$ echo $DISPLAY
:0.1 gvim ~/.conkyrc
 
  


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
bash scripting ZAMO Linux - General 1 01-19-2009 02:25 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Doubt in awk scripting.... stalin2020 Programming 1 06-02-2008 07:20 AM
Scripting doubt heraklez Slackware 2 02-05-2006 02:48 PM
Doubt with a bash code linuxlover1234 Programming 10 03-30-2004 12:48 PM

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

All times are GMT -5. The time now is 04:35 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
Open Source Consulting | Domain Registration