开发者

Functions in bash with special parameters

in a bash script I am implementing some functions, that take parameters

The problem is when the par开发者_如何转开发ameters instaed of being MONDAY is END OF THE WEEK

How can I pass this parameter to the function so the function

function week{
TIME=$1
}

takes a $TIME "END OF THE WEEK" and not just "END"?


You can use:

TIME="$*"

to get all of the parameters strung together, as in:

#!/bin/bash
function week {
        TIME="$*"
        echo "${TIME}"
}
week end of the week

which produces (all four arguments used):

end of the week

If you want to preserve white space. you can pass it as a quoted string.

#!/bin/bash
function week {
        TIME="$*"
        echo "${TIME}"
}
week "end        of the week"

which produces (from a single argument):

end        of the week


Enclose a variable in double quotes to prevent word splitting but interpolate the variable's value. Single quoted strings undergo even less processing.

function week {
    TIME="$1"
}

week 'END OF THE WEEK'


function week {
  TIME="$1"
}


function week {
  TIME="$1"
}

--syntax editing slowed me down ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜