简体   繁体   中英

how to make a multiple string to single string in linux shell scripting

Am having a string like ("50342364232 , Munish inspiring") but when am giving this as input it is taking as 3 string in linux shell so how to make it as a single string ? i gave the input like ./filename "50342364232 , Munish inspiring "

If you invoke a program like ./program "50342364232 , Munish inspiring" , including the quotes, then it will be interpreted as a single argument to program . However, if within program you do something like call other-program $1 , then when $1 is expanded, it will be expanded as multiple arguments. To work around that, you would want to invoke it as other-program "$1" , which will preserve it as a single argument.

If you mean from the command line, just use the $@ build in array reference, and be sure to quote it, eg:

$ malx "50342364232 , Munish inspiring" "some other, literal string" "and yet... one more" |sed 's/^/    /' # pad4 for forum
for larg in "$@";do
  ((++ct))
  echo "Local Arg $ct:  $larg"
done

Output:

Local Arg 1:  50342364232 , Munish inspiring
Local Arg 2:  some other, literal string
Local Arg 3:  and yet... one more

If you mean, how do you capture that in your script with the ability to extract it later the way you got it, I'd load an array (quoted, see below), then use the index generation feature in bash. If you use a '!' between the opening brace and array variable name when referencing the entire array "[@]," instead of resolving to the values, it resolves to the indexes of the array.

argary=("$@")    # command-line args
# can continue to add to array locally:
argary+=("Brown eggs"
         "are local eggs, and"
         "local eggs are fresh!")
for ix in ${!argary[@]};do
  echo "Element $ix:  ${argary[ix]}"
done

Output:

$ malx2 "50342364232 , Munish inspiring" "some other, literal string" "and yet... one more" |sed 's/^/    /' # forum formating
Element 0:  50342364232 , Munish inspiring
Element 1:  some other, literal string
Element 2:  and yet... one more
Element 3:  Brown eggs
Element 4:  are local eggs, and
Element 5:  local eggs are fresh!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM