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: ...
- 11-22-2008 #1Just 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
- 11-23-2008 #2Just Joined!
- Join Date
- Apr 2007
- Posts
- 32
- 11-23-2008 #3
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" ...


Reply With Quote
