LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   What is this Bash example trying to convey? (https://www.linuxquestions.org/questions/programming-9/what-is-this-bash-example-trying-to-convey-4175608033/)

justmy2cents 06-16-2017 01:09 PM

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


pan64 06-16-2017 01:19 PM

(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?

justmy2cents 06-16-2017 02:00 PM

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.

suicidaleggroll 06-16-2017 02:03 PM

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.

justmy2cents 06-16-2017 02:06 PM

Quote:

Originally Posted by suicidaleggroll (Post 5723518)
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 ;)

justmy2cents 06-16-2017 02:07 PM

THREAD SOLVED! Thanks Everyone.

justmy2cents 06-16-2017 02:08 PM

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?

suicidaleggroll 06-16-2017 02:27 PM

"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".

justmy2cents 06-16-2017 03:05 PM

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

astrogeek 06-16-2017 03:11 PM

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!

justmy2cents 06-16-2017 03:17 PM

Quote:

Originally Posted by astrogeek (Post 5723542)
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.

pan64 06-17-2017 01:55 AM

Quote:

Originally Posted by justmy2cents (Post 5723547)
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....

KenJackson 06-17-2017 10:03 AM

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



All times are GMT -5. The time now is 11:07 PM.