Shell script pass arguments with spaces

I want to pass arguments from one shell script (say script1) to another. Some arguments contain spaces. So I included quotes in the arguments and I repeated it before going to script2. This is how it is done,

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 script2, if I do this,

 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 anyone please fix my mistake? What should I do to get the mentioned argument in general.

0


source to share


2 answers


Embedding quotes in the value of a variable does nothing useful. As @Etan Reisner said, refer to http://mywiki.wooledge.org/BashFAQ/050 . In this case, the best answer is probably storing FL as an array, rather than a simple variable:

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

      



Note that quotes are not stored as part of the array elements; instead, they are used to force paths to be processed with single array elements rather than splitting into 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[@]}"

      

+2


source


1- Use the following (put "around FL"):

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

      

2- Then, inside your script2 use (to extract the variable based on the format you know about):



for arg; do # default for a for loop is to iterate over "$@"
   case $arg in
    '-filelist'*) input=${arg} ;;
      esac
done

      

3 You can now parse the input parameter in whatever format you want to use awk

.

0


source







All Articles