How to keep quotes in Bash arguments?


I have a Bash script where I want to keep quotes in the arguments passed.


./ this is "some test"

then I want to use those arguments, and re-use them, including quotes and quotes around the whole argument list.

I tried using \"$@\", but that removes the quotes inside the list.

How do I accomplish this?

12/19/2017 5:43:29 AM

Accepted Answer

Just use single quotes around the string with the double quotes:

./ this is '"some test"'

So the double quotes of inside the single quotes were also interpreted as string.

But I would recommend to put the whole string between single quotes:

 ./ 'this is "some test" '

In order to understand what the shell is doing or rather interpreting arguments in scripts, you can write a little script like this:


echo $@
echo "$@"

Then you'll see and test, what's going on when calling a script with different strings

6/27/2018 6:03:00 PM

using "$@" will substitute the arguments as a list, without re-splitting them on whitespace (they were split once when the shell script was invoked), which is generally exactly what you want if you just want to re-pass the arguments to another program.

What are you trying to do and in what way is it not working?

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow