Find the answer to your Linux question:
Results 1 to 3 of 3
I am using bash shell. My problem is with parameter substitution. I want this script to display four variables (or their value substitutions) using positional arguments $1-$4 as parameter in: ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    5

    using a variable inside parameter substitutions

    I am using bash shell. My problem is with parameter substitution. I want this script to display four variables (or their value substitutions) using positional arguments $1-$4 as parameter in: "parameter:-value"
    but I get a "bad substitution" when I use a "$" inside the curly braces... Is there any way to do this without having the user type "$" before each variable when they run the command?

    Here is my code:

    #display first variable; if empty or not set, display "NO VAR1"
    echo ${1:-"NO VAR1"}

    #display second variable; if empty, assign "NOW V2" to it and #display

    : ${2:="NOW V2"}
    echo ${2}

    #if third variable is set, display "VAR3 SET"
    echo ${3:+"VAR3 SET"}

    #display fourth variable; if empty, display "Oh NO!" and exit
    echo ${4: ?"Oh NO!"}

    echo all done

  2. #2
    Just Joined!
    Join Date
    Apr 2007
    Posts
    32
    Quote Originally Posted by doggybah View Post
    I am using bash shell. My problem is with parameter substitution. I want this script to display four variables (or their value substitutions) using positional arguments $1-$4 as parameter in: "parameter:-value"
    but I get a "bad substitution" when I use a "$" inside the curly braces... Is there any way to do this without having the user type "$" before each variable when they run the command?

    Here is my code:

    #display first variable; if empty or not set, display "NO VAR1"
    echo ${1:-"NO VAR1"}

    #display second variable; if empty, assign "NOW V2" to it and #display

    : ${2:="NOW V2"}
    echo ${2}

    #if third variable is set, display "VAR3 SET"
    echo ${3:+"VAR3 SET"}

    #display fourth variable; if empty, display "Oh NO!" and exit
    echo ${4: ?"Oh NO!"}

    echo all done
    Hi You can do as fallows

    var1=32
    echo $var1
    var2=31
    echo $var2

  3. #3
    Linux User dxqcanada's Avatar
    Join Date
    Sep 2006
    Location
    Canada
    Posts
    259
    There is a problem with one of the statements:

    ${parameter:=word}
    If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.



    Men occasionally stumble over the truth,
    but most of them pick themselves up
    and hurry off as if nothing had happened.

    Winston Churchill


    ... then the Unix-Gods created "man" ...

Posting Permissions

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