Results 1 to 3 of 3

Thread: simple default variable

  1. #1
    Join Date
    Mar 2012
    Beans
    11

    simple default variable

    I know this is not correct. I want the option variable to be the default if the option is not given on the command line.

    Code:
    #!/bin/bash
    
    foo2option=1
    foo1 $(foo2 --foo2option $2 $1)
    Running the script with no option:
    Code:
    $ ./script somefile
    would run:
    Code:
    foo1 $(foo2 --foo2option 1 somefile)
    Running the script with an option:
    Code:
    $ ./script somefile 2
    would run:
    Code:
    foo1 $(foo2 --foo2option 2 somefile)

  2. #2
    Join Date
    Apr 2012
    Beans
    7,068

    Re: simple default variable

    Have a look at the bash

    Code:
    ${parameter-default}, ${parameter:-default}
    syntax - that should do it - something like

    Code:
    #!/bin/bash
    
    default=2
    
    echo command $1 ${2:-$default}
    will use the 2nd command line argument $2 if it is set, and $default otherwise

    http://tldp.org/LDP/abs/html/paramet...stitution.html

    I hope this is legal! if not I'm sure one of the real bashistas will step in and correct me

  3. #3
    Join Date
    Mar 2012
    Beans
    11

    Re: simple default variable

    Quote Originally Posted by steeldriver View Post
    will use the 2nd command line argument $2 if it is set, and $default otherwise

    http://tldp.org/LDP/abs/html/paramet...stitution.html

    I hope this is legal! if not I'm sure one of the real bashistas will step in and correct me
    Perfect! Thanks steeldriver. I need to read more about syntax. I'm marking this solved. If anyone still wants to throw in other ideas, feel free.

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
  •