1

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.

2
  • 1
    mywiki.wooledge.org/BashFAQ/050 Don't stick quoted strings inside variables and expect them to work, they won't. Just pass the arguments directly. Commented Jan 29, 2015 at 16:10
  • How do I set up the FL variable ? Like this -filelist /Users/armv7/My build/normal/My build.LinkFilelist -filelist /Users/arm64/My build/normal/My build.LinkFilelist ? Commented Jan 29, 2015 at 16:23

2 Answers 2

3

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[@]}"
Sign up to request clarification or add additional context in comments.

Comments

0

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 that you are aware of):

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

3- Now you can break the input parameter to whatever format you want using awk.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.