string - Concatenating remaining arguments beyond the first N in bash -


i did not have write bash script before. here need do.

my script run set of string arguments. number of stings more 8. have concatenate strings 9 , onward , make single string those. this...

myscript s1 s2 s3 s4 s5 s6 s7 s8 s9 s10....(total unknown)

in script, need this...

new string = s9 + s10 + ...

i trying this...(from web search).

 array="${@}"  tlen=${#array[@]}  # use loop  read string beyond 9  (( i=8; i<${tlen}; i++ ));     echo ${array[$i]}  --> show string beyond 9  done 

not working. prints out if i=0. here input.

./tastest 1 2 3 4 5 6 7 8 b c

i expecting b c printed. have make abc.

can help?

it should lot simpler looping in question:

shift 8 echo "$*" 

lose arguments 1-8; print other arguments single string single space separating arguments (and spaces within arguments preserved).

or, if need in variable, then:

nine_onwards="$*" 

or if can't throw away first 8 arguments in main shell process:

nine_onwards="$(shift 8; echo "$*")" 

you can check there @ least 9 arguments, of course, complaining if there aren't. or can accept empty string instead — no error.

and if arguments must concatenated no space (as in amendment question), have juggle $ifs:

nine_onwards="$(shift 8; ifs=""; echo "$*")" 

if i'm interpreting comments below answer correctly, want save first 8 arguments in 8 separate simple (non-array) variables, , arguments 9 onwards in simple variable no spaces between argument values.

that's trivially doable:

var1="$1" var2="$2" var3="$3" var4="$4" var5="$5" var6="$6" var7="$7" var8="$8" var9="$(shift 8; ifs=""; echo "$*")" 

the names don't have closely related are. use:

teflon="$1" absinthe="$2" astronomy="$3" lobster="$4" darkest_peru="$5" mp="$6" culinary="$7" dogma="$8" concatenation="$(shift 8; ifs=""; echo "$*")" 

you don't have them in order, either; sequence (permutation) nicely.

note, too, in question, have:

array="${@}" 

despite name, creates simple variable containing arguments. create array, must use parentheses this, spaces optional:

array=( "$@" ) 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -