help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Tackling parameter expansion in functions


From: Richard Taubo
Subject: Re: [Help-bash] Tackling parameter expansion in functions
Date: Fri, 12 Feb 2016 19:15:33 +0100

> Feb 12, 2016 kl. 2:09 PM skrev Greg Wooledge <address@hidden>:
> 
> On Fri, Feb 12, 2016 at 12:56:22PM +0100, Richard Taubo wrote:
>> # ----------------- This Works 
>> ----------------------------------------------------
>> 
>> Test() {
>>      printf "my_word" | head -c${1:-7}
>> }
>> 
>> Test
> 
> Since there is no argument in this case, ${1:-7} expands to 7.
> So you are runing ... | head -c7
> 
>> # ----------------- Does not work ---------------------------------------
>> Test2() {
>>      printf "$1" | head -c${1:-7}
>> }
>> Test2 "my_word"
> 
> There is an argument in this case, so ${1:-7} expands to my_word.
> So you are running ... | head -cmy_word
> 
> It seems unlikely that you wanted to use $1 in both places inside
> the function.  Did you really mean to use $1 on the left and $2 on
> the right, perhaps?  (But neither of your examples showed two
> parameters.)

I see now that ${1:-7} does not make sense.
Thought first it was part of the available head commands, but that
obviously does not make much sense . . .

> What are you actually trying to do?
:-)

Something like:
---------------------
Test2() {
   if [ “$1” == "this" ]; then
       word=$("< /dev/urandom tr -dc _A-Z | head -c${1:-7} 2>/dev/null;echo;")
       printf "$word"
   fi
}

Test2 "this"
---------------------

Thanks for feedback!


Bestr regards,
Richard Taubo











reply via email to

[Prev in Thread] Current Thread [Next in Thread]