You are not logged in.

#1 2008-09-27 16:25:51

ezzetabi
Member
Registered: 2006-08-27
Posts: 905

bash ${var} vs "$var"

When you make a package when you can not be totally sure that the variable contents does not contain spaces you should use "$var" instead of $var or ${var}. Actually the ${var}  is only useful if need some replacement function you can read about in the man.

To see the difference in action try:

$ A="bye bye bye"; touch ${A}

and

$ A="bye bye bye"; touch "$A"

Otherwise try:

$ mkdir a\ a\ a
$ cd a\ a\ a

and inside create a PKGBUILD with this contents:

pkgname=a_a_a
pkgver=1
pkgrel=1
pkgdesc="ah ah ah"
arch=(i686 x86_64)
url=""
license=('WTFPL')
md5sums=()
build() {
  cd /
  cd ${startdir}/pkg
  touch "testing spaces"
  return 0 }

executing pkgbuild you'll see it does not work as intended. The second line does not override the first.
Instead using "$startdir" works flawlessly.
In fact the {} does nothing in this cases.

My advice is: if you are not really desperate for diskspace ALWAYS use "$VARIABLE" you can not go wrong. If you are desperate use "$VARIABLE" every time you can not be sure the variable will never contains spaces or other dividing characters (e.g. $pkgver).


Ok, flame war ON!

Edit: Little mistake fixed. But it does not change the whole meaning.

Last edited by ezzetabi (2008-09-27 18:18:34)

Offline

#2 2008-09-27 17:52:29

thayer
Fellow
From: Vancouver, BC
Registered: 2007-05-20
Posts: 1,560
Website

Re: bash ${var} vs "$var"

ezzetabi wrote:

My advice is: if you are not really desperate for diskspace ALWAYS use "$VARIABLE" you can not go wrong. If you are desperate use "$VARIABLE" every time you can not be sure the variable will never contains spaces or other dividing characters (e.g. $pkgver).

You mean... ALWAYS use ${VARIABLE} you can not go wrong...and I agree.


thayer williams ~ cinderwick.ca

Offline

#3 2008-09-27 17:56:18

ezzetabi
Member
Registered: 2006-08-27
Posts: 905

Re: bash ${var} vs "$var"

I guess you read the whole message... There are two counter examples.

Edit:
An example where the {} are actually useful.
If the package version contained dashes, you have to change them to underscores:
good_program v10-pre8
becomes
pkgver='10_pre8'
if you need to use the $pkgver variable somewhere, for example in the source field, you can and should use ${pkgver//_/-}, in the example:
source=(http://a.invalid/dir/$pkgname-v${pkgver//_/-}.tar.gz)

here the {} do something, they replace all the _ with -.

Last edited by ezzetabi (2008-09-27 18:39:57)

Offline

#4 2008-09-27 20:10:36

thayer
Fellow
From: Vancouver, BC
Registered: 2007-05-20
Posts: 1,560
Website

Re: bash ${var} vs "$var"

You're right, I didn't read the whole message, or rather I didn't understand the meaning the first time around.  I've never had any trouble with variable use within PKGBUILD files, but in general I do wrap the curly brackets around variables since it defines the variable explicitly (e.g. conjoining chars outside the brackets are not evaluated).  Then again, I'm not a programmer smile


thayer williams ~ cinderwick.ca

Offline

#5 2008-09-28 03:36:53

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: bash ${var} vs "$var"

Quoting and the ${} have nothing to do with each other. More often you will use it so you can "echo ${foo}bar", which uses $foo, whereas "echo $foobar" obviously would not work.

Offline

#6 2008-09-28 08:15:04

ezzetabi
Member
Registered: 2006-08-27
Posts: 905

Re: bash ${var} vs "$var"

@thayer
Leaving bug around because you had no problems so far is not wise.

@Daenyth
The problem comes out if you the variable contains spaces and you call a program where the number of arguments is important... Just like the examples I put.
Moreover your example actually can solved also quoting "$foo"bar works correctly. An other reason to use "$var".

Guys, always use quotes and use ${} only if you know what are you doing. Knowing what you are doing means using one of the ability of {}, like array indexing, replacement, using {!, ect... (read man bash).

Offline

#7 2008-09-28 14:06:58

pelle.k
Member
From: Åre, Sweden (EU)
Registered: 2006-04-30
Posts: 667

Re: bash ${var} vs "$var"

The problem comes out if you the variable contains spaces and you call a program where the number of arguments is important... Just like the examples I put.

But you would never do

A="bye bye bye"; touch ${A}

because you're supposed to it like this

A="bye bye bye"; touch "${A}"

The examples you presented in the first post are unfair, since the curly brackets variable isn't enclosed in quotes, while the variable without curly brackets is.


"Your beliefs can be like fences that surround you.
You must first see them or you will not even realize that you are not free, simply because you will not see beyond the fences.
They will represent the boundaries of your experience."

SETH / Jane Roberts

Offline

#8 2008-09-28 14:12:15

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: bash ${var} vs "$var"

@ezzetabi: A valid use case for ${} without array indexing, replacement, and so on, is if you want to do:

MYVAR1="some words perhaps with space"
MYVAR2="a few words before ${MYVAR1}s but not so many"

You could find a way to do that using only "$MYVAR2" instead, but why bother?

Granted this kind of use case doesn't come up in PKGBUILDs. But I'd try to explain to people the difference between these, rather than tell them to always use one of them.

${VAR} is a way to separate the specification of your variable name from the surrounding text. For instance, you may want to say ${VAR}more_text. When the surrounding text isn't valid in a variable name (e.g., if it's spaces), then the ${} isn't needed. But it's a helpful habit to always use it anyway.

"$VAR" and "${VAR}" are ways to chunk together the values stored in VAR. If you're passing this value to a command or another variable, e.g. in:

ANOTHERVAR="$VAR"
rm -f "$VAR"

then if there's any chance that VAR has whitespace in it, you should use surrounding quotes. Otherwise here's what happens. Suppose VAR="first second". Then:

ANOTHERVAR=$VAR
rm -f $VAR

will be read as:

ANOTHERVAR=first second
rm -f first second

which means: set ANOTHERVAR to "first" and execute the command "second", and try to rm each of the files "first" and "second". There are times where that is what you want to do. But usually you'd want to do this instead:

ANOTHERVAR="first second"
rm -f "first second"

and to get that result you have to surround your $VAR (or ${VAR}) in double quotes.

Last edited by Profjim (2008-09-28 14:13:42)

Offline

#9 2008-09-28 14:14:10

JGC
Developer
Registered: 2003-12-03
Posts: 1,658

Re: bash ${var} vs "$var"

I always use ${} to enclose variables. Not because there's need to, but because of consistency. $pkgname-$pkgver will work fine, so will $pkgname/$pkgver. But what about $pkgname1$pkgver? Bash can't know I want ${pkgname}1${pkgver} where the "1" char isn't part of my variable name.

Offline

#10 2008-09-28 15:11:55

ezzetabi
Member
Registered: 2006-08-27
Posts: 905

Re: bash ${var} vs "$var"

@pelle.k
Did you ever read a PKGBUILD file? Or read a WIKI article? The example you say you'd never do is pretty common. So the point of the thread was saying: People, do not do that! It may fail, and if it fail understanding the bug will be hard. Why bother?

@Profjim
This is a nice example of when you can use reasonably use ${} I did not think about. Good one!
Yet, it is pretty rare. For sake of curiosity I usually use:
MYVAR2='a few words before '"$MYVAR1"'s but not so many'
Thanks for being costructive. smile I had little hope anyone would at least try to understand why I made this thread.

@JGC
${pkgname}1${pkgver} will not work if there are spaces in $pkgname or $pkgver, in this very case you can be pretty sure it won't happen. Yet "$pkgname"1"$pkgver" will always work. But `the sake of consistency'  will bring soon or later to ${stardir} that will fail is there are spaces.
The point of the whole thread is saying to use "$V".

Last edited by ezzetabi (2008-09-28 15:14:39)

Offline

#11 2008-09-28 17:35:16

pelle.k
Member
From: Åre, Sweden (EU)
Registered: 2006-04-30
Posts: 667

Re: bash ${var} vs "$var"

The example you say you'd never do is pretty common.

What i'm saying is that you should never expand *variables* without quotes, unless there's a specific *need* for it. In your first (first post) comparison, the curly brackets variable failed because you didn't include *quotes*.
On the matter about curly brackets, i just thought it was common practice to use them, like in the case projim pointed out.


"Your beliefs can be like fences that surround you.
You must first see them or you will not even realize that you are not free, simply because you will not see beyond the fences.
They will represent the boundaries of your experience."

SETH / Jane Roberts

Offline

#12 2008-09-28 17:44:41

pelle.k
Member
From: Åre, Sweden (EU)
Registered: 2006-04-30
Posts: 667

Re: bash ${var} vs "$var"

Yet "$pkgname"1"$pkgver" will always work

Wouldn't "${pkgname}1${pkgver}" work just as well?

Last edited by pelle.k (2008-09-28 17:46:26)


"Your beliefs can be like fences that surround you.
You must first see them or you will not even realize that you are not free, simply because you will not see beyond the fences.
They will represent the boundaries of your experience."

SETH / Jane Roberts

Offline

#13 2008-09-28 19:34:23

ezzetabi
Member
Registered: 2006-08-27
Posts: 905

Re: bash ${var} vs "$var"

Double post... yeah.

Wouldn't "${pkgname}"'1'"${pkgver}" work just as well?
Wouldn't "${pkgname}""1""${pkgver}" work just as well?
Wouldn't "$pkgname"1$pkgver work just as well?
Wouldn't "$pkgname"'1'"${pkgver}" work just as well?
Wouldn't "${pkgname}'1'"$pkgver" work just as well?

It is becoming a troll diatribe. Do what you want.

Offline

#14 2008-09-28 20:15:50

pelle.k
Member
From: Åre, Sweden (EU)
Registered: 2006-04-30
Posts: 667

Re: bash ${var} vs "$var"

Double post... yeah.

Sorry about that.

It is becoming a troll diatribe. Do what you want.

I'm sorry if i have offended you in any way. That was not my intention.
Please, continue, i wont interfere any more smile

EDIT; btw, i see your point now. (i'm kind of tired). Bottom line, always quote variables kids! I apologize for complicating things...

Last edited by pelle.k (2008-09-28 22:42:15)


"Your beliefs can be like fences that surround you.
You must first see them or you will not even realize that you are not free, simply because you will not see beyond the fences.
They will represent the boundaries of your experience."

SETH / Jane Roberts

Offline

#15 2008-09-29 04:50:28

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: bash ${var} vs "$var"

I dont know who, but I'm fairly sure it was someone with a big hat who said that using curly-brackets is recommended in all situations or is the proper way to do things...

Last edited by iphitus (2008-09-29 04:51:07)

Offline

#16 2008-09-29 05:22:47

omp
Member
From: Glendale, California
Registered: 2005-10-02
Posts: 41
Website

Re: bash ${var} vs "$var"

I always stick to "${FOO}", with "${FOO}${BAR}" for multiple variables.


boo!

Offline

#17 2008-09-29 08:15:49

ezzetabi
Member
Registered: 2006-08-27
Posts: 905

Re: bash ${var} vs "$var"

@iphitus
Nor you know who, nor you know why...

@omp
That's totally safe of course, but the syntax becomes pretty heavy.
"$foo" variable name + 3 extra chars
"${foo}" variable name + 5 extra chars.
The problem decreases if there are multiple vars. Sure.
I am not saying it is wrong, I decided to make this topic because too often I see ${foo} or $foo where foo can contains space and breaking the code/build.

And sorry for making the topic, do what you want.

Last edited by ezzetabi (2008-09-29 08:18:56)

Offline

#18 2008-09-29 15:27:06

drag0nl0rd
Trusted User (TU)
From: Czech Republic
Registered: 2007-10-24
Posts: 125
Website

Re: bash ${var} vs "$var"

ezzetabi wrote:

And sorry for making the topic, do what you want.

You don't need to apologize, I found this conversation very useful. Thanks for starting it.

Offline

#19 2008-09-29 17:30:17

omp
Member
From: Glendale, California
Registered: 2005-10-02
Posts: 41
Website

Re: bash ${var} vs "$var"

ezzetabi wrote:

I am not saying it is wrong, I decided to make this topic because too often I see ${foo} or $foo where foo can contains space and breaking the code/build.

Indeed, I see this in Gentoo a lot with archaic ebuilds, or those on the bug tracker submitted by users.


boo!

Offline

#20 2008-09-30 14:11:25

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: bash ${var} vs "$var"

Try the same with zsh and realize how stupid bash is, creating all that confusion for nothing.


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

Board footer

Powered by FluxBB