Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-09-2010, 02:39 AM   #1
LQ Newbie
Registered: Mar 2010
Posts: 7

Rep: Reputation: 0
Bash scripting doubt

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"

var="My name is Bala"
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..

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

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 03:15 AM   #3
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
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, 05:08 AM   #4
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, 06:49 AM   #5
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,079

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

When you do this:

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

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:

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:

$ 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


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

All times are GMT -5. The time now is 10:11 AM.

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