Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

bash - Shell script pass arguments with spaces

I want to pass arguments from one shell script ( say script1 ) to another. Some of the arguments contain spaces. So I included quotes in the arguments and before passing to script2, I echoed it. Here is how it is,

echo $FL gives
-filelist "/Users/armv7/My build/normal/My build.LinkFilelist" -filelist "/Users/arm64/My build/normal/My build.LinkFilelist"

But when I do

script2  -arch armv7 -arch arm64 -isysroot /Applications/blahblah/iPhoneOS8.1.sdk $FL

and in the script2 if I do,

 for var in "$@"
  do
      echo "$var"
  done

I still get

"-arch"
"armv7"
"-arch"
"arm64"
"isysroot"
"/Applications/blahblah/iPhoneOS8.1.sdk"
"-filelist"
""/Users/armv7/My"
"build/normal/My"            // I want all these 3 lines together
build.LinkFilelist"" 
"-filelist"
""/Users/arm64/My"
"build/normal/My"
build.LinkFilelist""

Can someone please correct my error ? What should I do to get the mentioned argument as a whole.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Embedding quotes in a variable's value doesn't do anything useful. As @Etan Reisner said, refer to http://mywiki.wooledge.org/BashFAQ/050. In this case, the best answer is probably to store FL as an array, rather than a plain variable:

FL=(-filelist "/Users/armv7/My build/normal/My build.LinkFilelist" -filelist "/Users/arm64/My build/normal/My build.LinkFilelist")

Note that the quotes aren't stored as part of the array elements; instead, they're used to force the paths to be treated single array elements, rather than broken up by the spaces. Then refer to it with "${FL[@]}", which makes bash treat each element as an argument:

script2 -arch armv7 -arch arm64 -isysroot /Applications/blahblah/iPhoneOS8.1.sdk "${FL[@]}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...