LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-06-2007, 04:12 PM   #1
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 208Reputation: 208Reputation: 208
bash: extra stuff in brace expansion


In /etc/init.d/functions there are a bunch of variables which are used like this:

Code:
${VARIABLE:-}
how is this any different from just
Code:
${VARIABLE}
?

thanks,
jameson
 
Old 09-06-2007, 04:24 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Google this:
Code:
bash brace expansion
and all will be revealed.
 
Old 09-06-2007, 04:37 PM   #3
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Original Poster
Rep: Reputation: 208Reputation: 208Reputation: 208
duly noted - "shell parameter expansion"

learning bash is painful.
 
Old 09-06-2007, 04:58 PM   #4
rtspitz
Member
 
Registered: Jan 2005
Location: germany
Distribution: suse, opensuse, debian, others for testing
Posts: 307

Rep: Reputation: 32
http://www.faqs.org/docs/bashman/bashref_29.html
 
Old 09-07-2007, 03:44 AM   #5
fvu
LQ Newbie
 
Registered: Nov 2006
Distribution: Ubuntu 7.10
Posts: 28

Rep: Reputation: 15
From Bash Reference `Shell Parameter Expansion':
${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
One use of this construction is to let bash now it is okay when the variable is not set.

At first glance there seems no difference:
Code:
$> unset foo  # Make sure $foo is unset
$> echo -n $foo
$> echo -n ${foo:-}
$>
Difference is when you have set bash to trigger an error when a variable is unset via `set -o nounset'. This is one of the settings (besides `set -o errexit') I usually switch on in order to write neater code. This forces me to always initialize variables: it is like setting E_STRICT in PHP. Now with ${foo:-} you can let bash now it is okay when the variable is not set.

Code:
$> set -o nounset         # Trigger error when variable is not set.  Same as `set -u'
$> echo -n $foo           # Triggers error
bash: foo: unbound variable
$> echo -n ${foo:-}       # Doesn't trigger error: $foo is replaced with empty word
$> echo ${foo:-unset}     # Doesn't trigger error: $foo is replaced with word `unset'
unset
$>
See also:
http://www.fvue.nl/wiki/Bash:_Error_...unset_variable - Bash: Error `Unbound_variable' when testing unset variable

Freddy Vulto
http://fvue.nl/wiki/Bash

Last edited by fvu; 09-07-2007 at 03:58 AM.
 
  


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
!command expansion in csh vs. bash jhwilliams Linux - Software 9 08-27-2007 06:04 PM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 11:38 AM
Installing stuff from /extra Austin newbe Slackware 2 12-08-2006 05:43 AM
Bash variables expansion olaola Linux - Newbie 4 10-16-2006 12:45 PM
bash script $n variable expansion cortez Programming 6 12-08-2003 05:03 PM


All times are GMT -5. The time now is 12:49 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration