133

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

Example:

./test.sh this is "some test"

Then I want to use those arguments, and reuse 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?

6

14 Answers 14

147

Using "$@" will substitute the arguments as a list, without resplitting 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 repass the arguments to another program.

Note that this is a special form and is only recognized as such if it appears exactly this way. If you add anything else in the quotes the result will get combined into a single argument.

18
  • 7
    I think he means the quotes are part of the argument string, not part of the bash syntax.
    – Alex Brown
    May 12, 2014 at 17:46
  • 4
    @qwertzguy is incorrect. The double quotes will be stripped before the variable enters the arguments list. Nov 3, 2014 at 9:35
  • 11
    This is the top-rated answer. While it is correct per se, it doesn't address the question really. Jan 25, 2016 at 22:10
  • 2
    @TorstenBronger: Since the question is completely unclear, this is answering my best guess for what the OP actually wants.
    – Chris Dodd
    Jan 26, 2016 at 3:17
  • 2
    @Hofi: Those are all because you need the correct quoting for echo -- xargs and "$@" are irrelevant. Try echo -ne "nospace\x0yes space\x0" as I suggested.
    – Chris Dodd
    Apr 15, 2016 at 14:25
75

There are two safe ways to do this:

1. Shell parameter expansion: ${variable@Q}:

When expanding a variable via ${variable@Q}:

The expansion is a string that is the value of parameter quoted in a format that can be reused as input.

Example:

$ expand-q() { for i; do echo ${i@Q}; done; }  # Same as for `i in "$@"`...
$ expand-q word "two words" 'new
> line' "single'quote" 'double"quote'
word
'two words'
$'new\nline'
'single'\''quote'
'double"quote'

2. printf %q "$quote-me"

printf supports quoting internally. The manual's entry for printf says:

%q Causes printf to output the corresponding argument in a format that can be reused as shell input.

Example:

$ cat test.sh
#!/bin/bash
printf "%q\n" "$@"
$
$ ./test.sh this is "some test" 'new
>line' "single'quote" 'double"quote'
this
is
some\ test
$'new\nline'
single\'quote
double\"quote
$

Note the second way is a bit cleaner if displaying the quoted text to a human.

Related: For Bash, POSIX sh and Z shell (executable zsh): Quote string with single quotes rather than backslashes

9
  • 1
    This worked for me. In a function I used: local _quoted=$( printf ' "%q"' "$@" ) This answer looks like the most robust. Nov 7, 2017 at 8:50
  • 8
    The 1st method expand-q has an error: ${i@Q} should be ${i:Q} otherwise it fails with bash: ${i@Q}: bad substitution
    – arielf
    May 31, 2018 at 0:49
  • 3
    @arielf What version of bash? It works as shown on $BASH_VERSION 4.4.19(1)-release. What you're doing is probably a side effect of ${parameter:offset} where offset is 0, (possibly because atoi() of Q gives 0).
    – Tom Hale
    May 31, 2018 at 8:30
  • 2
    That may be the reason. I'm on Ubuntu 16.04.4 (LTS). Latest bash on this system is 4.3.48(1)-release
    – arielf
    Jun 2, 2018 at 4:47
  • 9
    I found ${*@Q} to be very useful for passing all command line arguments to a subsequent shell invocation, in my case one due to a sg call to change group. Thanks for this answer!
    – MvG
    Aug 30, 2019 at 21:25
49

Yuku's answer only works if you're the only user of your script, while Dennis Williamson's answer is great if you're mainly interested in printing the strings, and expect them to have no quotes-in-quotes.

Here's a version that can be used if you want to pass all arguments as one big quoted-string argument to the -c parameter of bash or su:

#!/bin/bash
C=''
for i in "$@"; do
    i="${i//\\/\\\\}"
    C="$C \"${i//\"/\\\"}\""
done
bash -c "$C"

So, all the arguments get a quote around them (harmless if it wasn't there before, for this purpose), but we also escape any escapes and then escape any quotes that were already in an argument (the syntax ${var//from/to} does global substring substitution).

You could of course only quote stuff which already had whitespace in it, but it won't matter here. One utility of a script like this is to be able to have a certain predefined set of environment variables (or, with su, to run stuff as a certain user, without that mess of double-quoting everything).


I had reason to do this in a POSIX way with minimal forking, which lead to this script (the last printf there outputs the command line used to invoke the script, which you should be able to copy-paste in order to invoke it with equivalent arguments):

#!/bin/sh
C=''
for i in "$@"; do
    case "$i" in
        *\'*)
            i=`printf "%s" "$i" | sed "s/'/'\"'\"'/g"`
            ;;
        *) : ;;
    esac
    C="$C '$i'"
done
printf "$0%s\n" "$C"

I switched to '' since shells also interpret things like $ and !! in ""-quotes.

7
  • Use array instead? I'll post an example below.
    – JohnMudd
    Apr 1, 2015 at 18:45
  • 1
    This is almost perfect, but I would suggest one minor modification - escaping all ` before appending to $C` - i.e. adding i="${i//\\/\\\\}" otherwise arguments like "\\\"asdf" will fail. Jul 24, 2016 at 16:32
  • 1
    @unhammer Thank you so much, this works awesome! Jan 10, 2019 at 20:51
  • 2
    ℹ️ Same as just: "${@@Q}" May 19, 2021 at 1:09
  • 1
    man bash says it's a Parameter transformation. Could use that in the bash-version instead of the regex replace, giving C=''; for i in "$@"; do C="$C ${i@Q}"; done; bash -c "$C"
    – unhammer
    May 19, 2021 at 10:35
37

If it's safe to make the assumption that an argument that contains white space must have been (and should be) quoted, then you can add them like this:

#!/bin/bash
whitespace="[[:space:]]"
for i in "$@"
do
    if [[ $i =~ $whitespace ]]
    then
        i=\"$i\"
    fi
    echo "$i"
done

Here is a sample run:

$ ./argtest abc def "ghi jkl" $'mno\tpqr' $'stu\nvwx'
abc
def
"ghi jkl"
"mno    pqr"
"stu
vwx"

You can also insert literal tabs and newlines using Ctrl-V Tab and Ctrl-V Ctrl-J within double or single quotes instead of using escapes within $'...'.

A note on inserting characters in Bash: If you're using Vi key bindings (set -o vi) in Bash (Emacs is the default - set -o emacs), you'll need to be in insert mode in order to insert characters. In Emacs mode, you're always in insert mode.

2
  • But how to check if the user has quoted say, the 1st argument? Nov 9, 2018 at 16:59
  • 2
    @TonyBarganski: The shell strips quotes when arguments are processed before they're passed to the program receiving them. It's not possible to tell whether there were outer quotes on an argument. Nov 9, 2018 at 18:27
24

I needed this for forwarding all arguments to another interpreter. What ended up right for me is:

bash -c "$(printf ' %q' "$@")"

Example (when named as forward.sh):

$ ./forward.sh echo "3 4"
3 4
$ ./forward.sh bash -c "bash -c 'echo 3'"
3

(Of course the actual script I use is more complex, involving in my case nohup and redirections etc., but this is the key part.)

0
14

Like Tom Hale said, one way to do this is with printf using %q to quote-escape.

For example:

send_all_args.sh

#!/bin/bash
if [ "$#" -lt 1 ]; then
 quoted_args=""
else
 quoted_args="$(printf " %q" "${@}")"
fi

bash -c "$( dirname "${BASH_SOURCE[0]}" )/receiver.sh${quoted_args}"

send_fewer_args.sh

#!/bin/bash
if [ "$#" -lt 2 ]; then
 quoted_last_args=""
else
 quoted_last_args="$(printf " %q" "${@:2}")"
fi

bash -c "$( dirname "${BASH_SOURCE[0]}" )/receiver.sh${quoted_last_args}"

receiver.sh

#!/bin/bash
for arg in "$@"; do
  echo "$arg"
done

Example usage:

$ ./send_all_args.sh
$ ./send_all_args.sh a b
a
b
$ ./send_all_args.sh "a' b" 'c "e '
a' b
c "e
$ ./send_fewer_args.sh
$ ./send_fewer_args.sh a
$ ./send_fewer_args.sh a b
b
$ ./send_fewer_args.sh "a' b" 'c "e '
c "e
$ ./send_fewer_args.sh "a' b" 'c "e ' 'f " g'
c "e
f " g
1
  • This is a great example because it contains a reproducible test for forwarding the arguments through something like sh -c. Plus, it works ;-). Not all of the others do. Sep 11, 2020 at 17:32
9

Just use:

"${@}"

For example:

# cat t2.sh
for I in "${@}"
do
   echo "Param: $I"
done
# cat t1.sh
./t2.sh "${@}"

# ./t1.sh "This is a test" "This is another line" a b "and also c"
Param: This is a test
Param: This is another line
Param: a
Param: b
Param: and also c
2
6

I changed unhammer's example to use array.

printargs() { printf "'%s' " "$@"; echo; };  # http://superuser.com/a/361133/126847

C=()
for i in "$@"; do
    C+=("$i")  # Need quotes here to append as a single array element.
done

printargs "${C[@]}"  # Pass array to a program as a list of arguments.
3
  • Is it possible to use "${C[@]}" as an argument to bash -c there? E.g. if your script was called with the arguments echo foo\"bar"'"fie
    – unhammer
    Feb 3, 2016 at 8:25
  • e.g. foo=( echo bar\"fie"'"fum ); C='';for i in "${foo[@]}"; do C="$C \"${i//\"/\\\"}\""; done; bash -c "$C" gives bar"fie'fum
    – unhammer
    Feb 3, 2016 at 8:28
  • 1
    Though the code looks cleaner, you still loose the quotes Oct 1, 2016 at 14:31
6

My problem was similar and I used mixed ideas posted here.

We have a server with a PHP script that sends e-mails. And then we have a second server that connects to the 1st server via SSH and executes it.

The script name is the same on both servers and both are actually executed via a bash script.

On server 1 (local) bash script we have just:

/usr/bin/php /usr/local/myscript/myscript.php "$@"

This resides on /usr/local/bin/myscript and is called by the remote server. It works fine even for arguments with spaces.

But then at the remote server we can't use the same logic because the 1st server will not receive the quotes from "$@". I used the ideas from JohnMudd and Dennis Williamson to recreate the options and parameters array with the quotations. I like the idea of adding escaped quotations only when the item has spaces in it.

So the remote script runs with:

CSMOPTS=()
whitespace="[[:space:]]"
for i in "$@"
do
    if [[ $i =~ $whitespace ]]
    then
        CSMOPTS+=(\"$i\")
    else
        CSMOPTS+=($i)
    fi
done

/usr/bin/ssh "$USER@$SERVER" "/usr/local/bin/myscript ${CSMOPTS[@]}"

Note that I use "${CSMOPTS[@]}" to pass the options array to the remote server.

2
  • the above slightly fails: -opt="value" becomes "-opt=value"
    – sdive
    Feb 6, 2017 at 13:58
  • I am not using = in my scripts, but since the \" are only added when spaces are detected, -opt="value" will actually become -opt=value. something like -opt="another value" would be sent as "-opt=another value" but since my setup sends it to another script on another machine it is still interpreted as a single token as intended. The remote script receives -opt=another value as a single token without the quotations. Things are very different if running locally though. You wouldn't even need to escape the \" for example.
    – Gus Neves
    Apr 19, 2017 at 14:55
3

Quotes are interpreted by Bash and are not stored in command line arguments or variable values.

If you want to use quoted arguments, you have to quote them each time you use them:

val="$3"
echo "Hello, World!" > "$val"
1

If you need to pass all arguments to Bash from another programming language (for example, if you'd want to execute bash -c or emit_bash_code | bash), use this:

  • escape all single quote characters you have with '\''.
  • then, surround the result with singular quotes

The argument of abc'def will thus be converted to 'abc'\''def'. The characters '\'' are interpreted as following: the already existing quoting is terminated with the first first quote, then the escaped singular single quote \' comes, then the new quoting starts.

6
  • 2
    No. Don't escape arguments as text embedded in the code; instead, use an interface from your other programming language that lets you pass an array of arguments, and pass each item in its own array element. This means folks shouldn't ever use system(); fortunately, its deficiencies have been recognized for decades, so it's rarely the only choice. Jun 9, 2020 at 13:00
  • In Python, for example, one can pass an array rather than a string to a subprocess-family call. In C, one can use execv or posix_spawn. In Java, one can use one of the array-based interfaces in Runtime.exec(); etc, etc. Jun 9, 2020 at 13:03
  • 1
    @CharlesDuffy Sorry, but my answer starts from the word "if". See "for example" if it's not clear enough... If you can just call a CLI by using an array of arguments, of course that's better. In the situation I describe, however, it's just not possible (bash -c or another_program | bash). Jun 9, 2020 at 19:01
  • 1
    It's absolutely possible. subprocess.Popen(['bash', '-c', 'code that uses "$1" and "$2"', "_", "value for $1", "value for $2"]), f/e, starts bash from Python while passing two values in a way that keeps them out-of-band from code. The pipe-to-bash pattern simply shouldn't be followed at all, but one can get code that way while similarly passing data out-of-band with any of the available equivalents to another_program | bash -s 'value for $1' 'value for $2' Jun 9, 2020 at 19:11
  • 1
    The pipe-to-bash pattern simply shouldn't be followed at all -- you could be right. That's what my answer is about still. I guess it might be wrong in a sense that, I should give more warnings on the preferred approach. Personally, I used it to print a CLI instruction that can be copy-pasted by the user to a terminal (again something that you should never really encourage, to be fair). Jun 10, 2020 at 9:30
1

As Gary S. Weaver has shown in his source code tips, the trick is to call Bash with parameter '-c' and then quote the next.

E.g.

bash -c "<your program> <parameters>"

or

docker exec -it <my docker> bash -c "$SCRIPT $quoted_args"
0

Yes, seems that it is not possible to ever preserve the quotes, but for the issue I was dealing with it wasn't necessary.

I have a Bash function that will search down folder recursively and grep for a string, the problem is passing a string that has spaces, such as "find this string". Passing this to the Bash script will then take the base argument $n and pass it to grep, this has grep believing these are different arguments. The way I solved this by using the fact that when you quote Bash to call the function it groups the items in the quotes into a single argument. I just needed to decorate that argument with quotes and pass it to the grep command.

If you know what argument you are receiving in bash that needs quotes for its next step you can just decorate with with quotes.

-18

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

./test.sh 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:

 ./test.sh '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:

#!/bin/bash

echo $@
echo "$@"

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

7
  • 60
    How can this be the accepted answer? This requires the caller of the script to know that he meeds double quotation although this can be handled in the script without altering the way the caller builds the parameter list Mar 2, 2016 at 7:09
  • 4
    If you have no control over ./test.sh then this is the correct answer, but if you are authoring ./test.sh, then the answer from Chris Dodd is the better solution.
    – sanscore
    Mar 29, 2016 at 8:26
  • 1
    Not just if you have no control over test.sh, but only in the scenario where test.sh is written to use eval unsafely is this appropriate. And frankly, if something is using eval unsafely, that's probably a pretty good reason to stop using that thing. Mar 9, 2017 at 23:03
  • 2
    Wtf is wrong with people here. How is this an accepted answer. It passes the responsibility to the caller. May 12, 2017 at 7:19
  • Although this is correct, it is basically "you don't"-type answer. It doesn't really answer the question. Nor does it adhere to the SO rules for answers. If anything, a more informative explanation of why you don't can be found here stackoverflow.com/a/10836225/4687565
    – Dimitry
    Jun 30, 2017 at 11:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.