2

For example here is test.sh

#!/bin/bash for i in "${@}"; do echo "${i}" done 

If you run ./test.sh one 'two' "three", you will get

one two three 

But what I want to get is:

one 'two' "three" 

How can I get this?

Thanks.

2
  • 2
    ./test.sh one "'two'" '"three"' :P Just to inform you, the problem is not in your script. bash calling this script would strip out the quotes... Commented Jun 25, 2015 at 7:49
  • Thanks for the info. But a user(of the script) usually don't know to do that. So there is no way to pass quoted arguments transparently in bash. Commented Jun 25, 2015 at 8:25

2 Answers 2

2

Bash strips the quotes from strings. Thus if you want quotes, you need to put them inside other quotes.

Doing:

./test.sh one "'two'" '"three"' 

should give you the result you want.

Another possibility is to escape your quotes so that bash knows that it is part of the string:

./test.sh one \'two\' \"three\" 

will work, so will:

./test.sh "one" "\'two\'" "\"three\"" 

Otherwise you can always add the quotes again in your script by doing:

echo "\"${i}\"" 

for example.

Sign up to request clarification or add additional context in comments.

Comments

2

you need to adapt your input. For example like this:

# ./test.sh one \'two\' \"three\" one 'two' "three" # 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.