Results 1 to 6 of 6

Thread: [BASH] What does the variable ${1-.} contain?

  1. #1
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    [BASH] What does the variable ${1-.} contain?

    Dear forum members,

    I was reading through a bash script of one of my coworkers, and I see that he uses the variable ${1-.}. When I echo it in the ternimal, it simply returns
    Code:
    $ echo ${1-.}
    .
    It looks like it does the same thing as $PWD

    My question is: Is there a difference between using ${1-.} and $PWD in a script?
    Last edited by sha1sum; April 11th, 2014 at 08:21 AM.
    Nunca te acostarás, sin saber una cosa más.

  2. #2
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: [BASH] What does the variable ${1-.} contain?

    $1 expands to the first positional parameter. ${1-.} is a parameter expansion: If the first positional parameter is unset then `.' is substituted.


    See:http://mywiki.wooledge.org/BashGuide/Parameters
    and from the manual:
    Code:
    man bash | less +/"Parameter Expansion"

  3. #3
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: [BASH] What does the variable ${1-.} contain?

    I get it. Thanks!
    Nunca te acostarás, sin saber una cosa más.

  4. #4
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: [BASH] What does the variable ${1-.} contain?

    One quick follow-up question:

    Is there a difference between ${1-.} and ${1:-.} ?

    Or in general between

    Code:
    ${var:-value}
    and
    Code:
    ${var-value}
    Nunca te acostarás, sin saber una cosa más.

  5. #5
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,148
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: [BASH] What does the variable ${1-.} contain?

    From http://www.gnu.org/software/bash/manual/bashref.html

    Omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  6. #6
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: [BASH] What does the variable ${1-.} contain?

    Thanks! You guys rock.
    Nunca te acostarás, sin saber una cosa más.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •