Recently, I wrote:
N=(${@:-0 1 2 3 4 5 6 7 8 9})
And I was little stunned that this was working as I hoped. N is an array, and it is assigned with $@, but if there is no positional parameters, that’s no input, then N will have an array of 0 to 9. The ${paramter:-word} is called as Use Default Values Expansion, from bash(1):
- ${parameter:-word}
- Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
Note
There is also a Assign Default Values, ${parameter:=word}. (2015-07-27T21:30:57Z)
This got me thinking, what if you want to do like
(($# == 0)) && : set the positional parameters
N=("$@")
How are you going to code in that way?
set builtin command is how you can set the values, for example:
set -- 1 -2 3
Simple as that and it also works within a function.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.