1

I have a script that logs the user argument list. This list is later processed by getopt.

If the script is started like:

./script.sh -a 'this is a sentence' -b 1

... and then I save "$@", I get:

-a this is a sentence -b 1

... without the single quotes. I think (because of the way Bash treats quotes) these are removed and are not available to the script.

For logging accuracy, I'd like to include the quotes too.

Can the original argument list be obtained without needing to quote-the-quotes?

tripleee
  • 175,061
  • 34
  • 275
  • 318
teracow
  • 229
  • 3
  • 9
  • Try: `"'this is a sentence'"` or `'\'this is a sentence\''` if you want to keep the quotes – Allan Jan 31 '19 at 06:55

1 Answers1

3

No, there is no way to obtain the command line from before the shell performed whitespace tokenization, wildcard expansion, and quote removal on it.

If you want to pass in literal quotes, try

./script.sh '"-a"' '"this is a sentence"' '"-b"' '"1"'

Notice also how your original command line could have been written

'./script.sh' '-a' 'this is a sentence' '-b' '1'
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Agree, I was trying to avoid quoting-the-quotes as it's a bit inconvenient for the end-users to do this. :) – teracow Jan 31 '19 at 06:58