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.

Haishan
  • 43
  • 4
  • 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... – anishsane Jun 25 '15 at 07: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. – Haishan Jun 25 '15 at 08:25

2 Answers2

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.

patapouf_ai
  • 17,605
  • 13
  • 92
  • 132
2

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

# ./test.sh one \'two\' \"three\"
one
'two'
"three"
#
vlp
  • 583
  • 3
  • 8