LinuxQuestions.org
Review your favorite Linux distribution.
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 06-16-2017, 01:09 PM   #1
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 164

Rep: Reputation: Disabled
What is this Bash example trying to convey?


Code:
parameter expansion     unset var          var=""          var="gnu" 

${var-default}          default             —                gnu 

${var:-default}         default           default            gnu 

${var+alternate}           —              alternate        alternate 

${var:+alternate}          —                 —             alternate 

${var?error}             error               —                gnu 

${var:?error}            error             error              gnu

Last edited by justmy2cents; 06-16-2017 at 01:20 PM.
 
Old 06-16-2017, 01:19 PM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,355

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
(I do not know where is it taken from)
We have now 3 columns with 3 different settings: var is unset, var is set, but the value is an empty string, var is equal to gnu.
we have 6 expressions on 6 lines and you can see how they evaluated using the 3 different var.
Code:
var is:                  unset    empty   gnu

${var-default}         default    —       gnu 
${var:-default}        default  default   gnu 
${var+alternate}          —    alternate alternate 
${var:+alternate}         —       —      alternate 
${var?error}            error     —       gnu 
${var:?error}           error    error    gnu
(- means nothing or empty)
Is this ok?

Last edited by pan64; 06-16-2017 at 01:20 PM.
 
Old 06-16-2017, 02:00 PM   #3
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 164

Original Poster
Rep: Reputation: Disabled
Thanks for replying pan64 that does clear some stuff up, but I just have two questions: is there ever a situation where you would put var="" in a script? I already know that var="gnu" maps gnu to the name var, but what's the point of having nothing mapped to var? And secondly, what does unset mean? I Googled unset but I couldn't find anything saying exactly what it was.

Last edited by justmy2cents; 06-16-2017 at 04:31 PM.
 
Old 06-16-2017, 02:03 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,488

Rep: Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090
Unset essentially deletes the variable.

var="" might be used as an initialization, or to clear out the contents of a variable without fully deleting it with unset.
 
Old 06-16-2017, 02:06 PM   #5
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 164

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Unset essentially deletes the variable.

var="" might be used as an initialization, or to clear out the contents of a variable without fully deleting it with unset.
Thanks for the timely response I get it now
 
Old 06-16-2017, 02:07 PM   #6
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 164

Original Poster
Rep: Reputation: Disabled
THREAD SOLVED! Thanks Everyone.
 
Old 06-16-2017, 02:08 PM   #7
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 164

Original Poster
Rep: Reputation: Disabled
Actually what do you guys think alternate, default and error mean? In the columns.. Like what is it referring to?
Also is iam="cool" an expansion, or is ${cool} the expansion?

Last edited by justmy2cents; 06-16-2017 at 02:17 PM.
 
Old 06-16-2017, 02:27 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,488

Rep: Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090
"default", "alternate", and "error" don't mean anything, that's just what they put in the expansion in the left column, you can replace them with any string.

iam="cool" is a variable assignment, you're setting the variable "iam" equal to the string "cool".
 
Old 06-16-2017, 03:05 PM   #9
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 164

Original Poster
Rep: Reputation: Disabled
After reading more on those parameter expansion options in man bash, and looking at examples i'm starting to understand.. Ok thanks man!

Last edited by justmy2cents; 06-16-2017 at 03:12 PM.
 
Old 06-16-2017, 03:11 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,337
Blog Entries: 1

Rep: Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329Reputation: 2329
Others may disagree, but in my opinion man bash is a really an excellent resource!

It is long (i.e. complete) and some people seem to resist navigating around man pages, but time learning your way around man bash, and working through your own test cases is time very well spent indeed!
 
Old 06-16-2017, 03:17 PM   #11
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 164

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
Others may disagree, but in my opinion man bash is a really an excellent resource!

It is long (i.e. complete) and some people seem to resist navigating around man pages, but time learning your way around man bash, and working through your own test cases is time very well spent indeed!
definitely I agree but only once you get an idea what you're looking at, and that usually requires (at least for me) a "more human" explanation.
 
Old 06-17-2017, 01:55 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,355

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Quote:
Originally Posted by justmy2cents View Post
definitely I agree but only once you get an idea what you're looking at, and that usually requires (at least for me) a "more human" explanation.
I used to say: man pages are really good, but only if you have forgotten something and need to check how it was really designed. But sometimes really hard to understand a tool just by reading its man page. bash is a really complex piece of software. If you have questions you can always ask here....
 
1 members found this post helpful.
Old 06-17-2017, 10:03 AM   #13
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, PCLinuxOS
Posts: 617

Rep: Reputation: 77
I got to wondering what's the difference between - and ? because the top two lines and the bottom two lines seem to have identical function.

So I ran "man bash" and searched for :- (by typing /:-) and found that they are almost identical but for two differences.
  • The dash expands normally whereas the question mark expands the error to both stdout and stderr.
  • If the "error" word is empty, "a message to that effect" results instead of empty.
I can't even redirect the error message to /dev/null.
Code:
$ unset x
$ echo ${x-alternate}
alternate
$ echo ${x-alternate} >/dev/null
$ echo ${x?alternate}
-bash: x: alternate
$ echo ${x?alternate} >/dev/null 2>&1
-bash: x: alternate
$ echo ${x?}
-bash: x: parameter null or not set

Last edited by KenJackson; 06-17-2017 at 10:05 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
[SOLVED] How to convey my thanks to a LQ member? vectrum LQ Suggestions & Feedback 4 11-13-2012 08:54 AM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
LXer: Creative Commons Artist Spotlight: Convey LXer Syndicated Linux News 0 09-13-2007 09:10 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM
?howto convey my requirement -- any guidance? pudhiyavan Linux - Networking 1 02-16-2004 12:24 PM


All times are GMT -5. The time now is 02:59 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