44

I am making a Bash script that will print and pass complex arguments to another external program.

./script -m root@hostname,root@hostname -o -q -- 'uptime ; uname -a'

How do I print the raw arguments as such?

-m root@hostname,root@hostname -o -q -- 'uptime ; uname -a'

Using $@ and $*, removes the single quotes around uptime ; uname -a which could cause undesired results. My script does not need to parse each argument. I just need to print / log the argument string and pass them to another program exactly how they are given.

I know I can escape the quotes with something like "'uptime ; uname -a'", but I cannot guarantee the user will do that.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aus
  • 1,394
  • 1
  • 14
  • 19
  • 1
    Can't gurantee that the user will escape the quotes. – aus May 31 '12 at 15:06
  • According to this answer: https://unix.stackexchange.com/a/472593/341661, although `echo $@` doesn't print out the quotes, $@ does "pass the quotes in background". I've tried this by writing 2 script: the 1st script passed "$@" to 2nd script, while the 2nd script prints "$1", and it worked – Nikolas Mar 03 '20 at 02:39
  • See also https://stackoverflow.com/questions/11456403/stop-shell-wildcard-character-expansion which asks more specifically about disabling `*` expansion (you can't do that either). – tripleee Aug 26 '21 at 09:36

8 Answers8

42

The quotes are removed before the arguments are passed to your script, so it's too late to preserve them. But you can preserve their effect when passing the arguments to the inner command, and reconstruct an equivalent quoted/escaped version of the arguments for printing.

For passing the arguments to the inner command "$@"—with the double quotes, $@ preserves the original word breaks, meaning that the inner command receives exactly the same argument list that your script did.

For printing, you can use the %q format in Bash's printf command to reconstruct the quoting. Note that this won't always reconstruct the original quoting, but it will construct an equivalent quoted/escaped string. For example, if you passed the argument 'uptime ; uname -a' it might print uptime\ \;\ uname\ -a or "uptime ; uname -a" or any other equivalent (see William Pursell's answer for similar examples).

Here's an example of using these:

printf "Running command:"
printf " %q" innercmd "$@" # note the space before %q -- this inserts spaces between arguments
printf "\n"
innercmd "$@"

If you have Bash version 4.4 or later, you can use the @Q modifier on parameter expansions to add quoting. This tends to prefer using single quotes (as opposed to printf %q's preference for escapes). You can combine this with $* to get a reasonable result:

echo "Running command: innercmd ${*@Q}"
innercmd "$@"

Note that $* mashes all arguments together into a single string with whitespace between them, which is normally not useful, but in this case each argument is individually quoted so the result is actually what you (probably) want. (Well, unless you changed IFS, in which case the "whitespace" between arguments will be the first character of $IFS, which may not be what you want.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
8

Use ${@@Q} for a simple solution. To test it, put the lines below in a script, bigQ.

#!/bin/bash
line="${@@Q}"
echo $line

Run:

./bigQ 1 a "4 5" b="6 7 8"
'1' 'a' '4 5' 'b=6 7 8'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
flick
  • 91
  • 1
  • 2
  • ```@Q``` is an expansion operation that stands for "The expansion is a string that is the value of parameter quoted in a format that can be reused as input." https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html – rodvlopes Jun 23 '21 at 13:16
  • 1
    The `@Q` expansion operator is Bash 4+ only, and will not work e.g. on MacOS which is still stuck on Bash 3. – tripleee Aug 26 '21 at 09:35
  • 2
    This is the only answer which actually preserves the quotes! I'm working in a situation where literally only this solution works thanks! – profPlum Jun 10 '22 at 12:50
  • Oddly, for me (`GNU bash, version 5.2.15(1)-release-(x86_64-pc-linux-gnu)`), `${@@Q}` expands to the quoted string of the first list element. I had to use `${*@Q}` instead. – Константин Ван Apr 20 '23 at 16:15
6

If the user invokes your command as:

./script 'foo'

the first argument given to the script is the string foo without the quotes. There is no way for your script to differentiate between that and any of the other methods by which it could get foo as an argument (eg ./script $(echo foo) or ./script foo or ./script "foo" or ./script \f\o""''""o).

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 1
    Interesting. Guess I am out of luck for printing, but it seems that passing `"$@"` will still allow the external program to parse the args correctly. – aus May 31 '12 at 15:19
4

If you want to print the argument list as close as possible to what the user probably entered:

#!/bin/bash
chars='[ !"#$&()*,;<>?\^`{|}]'
for arg
do
    if [[ $arg == *"'"* ]]
    then
        arg=\""$arg"\"
    elif [[ $arg == *$chars* ]]
    then
        arg="'$arg'"
    fi
    allargs+=("$arg")    # ${allargs[@]} is to be used only for printing
done
printf '%s\n' "${allargs[*]}"

It's not perfect. An argument like ''\''"' is more difficult to accommodate than is justified.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
1

As someone else already mentioned, when you access the arguments inside of your script, it's too late to know which arguments were quote when it was called. However, you can requote the arguments that contain spaces or other special characters that would need to be quoted to be passed as parameters.

Here is a Bash implementation based on Python's shlex.quote(s) that does just that:

function quote() {
  declare -a params
  for param; do
    if [[ -z "${param}" || "${param}" =~ [^A-Za-z0-9_@%+=:,./-] ]]; then
      params+=("'${param//\'/\'\"\'\"\'}'")
    else
      params+=("${param}")
    fi
  done
  echo "${params[*]}"
}

Your example slightly changed to show empty arguments:

quote -m root@hostname,root@hostname -o -q -- 'uptime ; uname -a' ''

Output:

-m root@hostname,root@hostname -o -q -- 'uptime ; uname -a' ''
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Helder Pereira
  • 5,522
  • 2
  • 35
  • 52
  • this does work in some cases, but fails in others. Almost, but not quite for my use case. – senorsmile Apr 16 '20 at 22:15
  • 1
    BTW, `function funcname() {` merges the POSIX sh syntax `funcname() {` and the legacy ksh syntax `function funcname {` in a way that's incompatible with *both* POSIX sh and legacy ksh, and for no benefit whatsoever. See https://wiki.bash-hackers.org/scripting/obsolete – Charles Duffy Dec 30 '21 at 17:13
1

In my case, I have tried to call Bash like script --argument="--arg-inner=1 --arg-inner2".

Unfortunately, the previous solutions don't help in my case.

The definitive solution was:

#!/bin/bash
# Fix given array argument quotation
function quote() {
    local QUOTED_ARRAY=()
    for ARGUMENT; do
        case ${ARGUMENT} in
            --*=*)
                QUOTED_ARRAY+=( "${ARGUMENT%%=*}=$(printf "%q" "${ARGUMENT#*=}")" )
                shift
            ;;
            *)
                QUOTED_ARRAY+=( "$(printf " %q" "${ARGUMENT}")" )
            ;;
        esac
    done
    echo ${QUOTED_ARRAY[@]}
}

ARGUMENTS="$(quote "${@}")"

echo "${ARGUMENTS}"

The result in the case of macOS is --argument=--arg-inner=1\ --arg-inner2 which is logically the same.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pavel Železný
  • 191
  • 2
  • 6
0

I have a very simple solution, and it also works in the sh shell:

File script1.sh

#!/bin/sh

concatenated_string=""

for ARG in "$@"; do
    concatenated_string="${concatenated_string}'${ARG}' "
done

echo "${concatenated_string}"
eval "${concatenated_string}"

When I invoke script1.sh with: ./script1.sh sh -c "serve -l 9527 -s dist"

The output will be:

'sh' '-c' 'serve -l 9527 -s dist'

   ┌───────────────────────────────────────────────────┐
   │                                                   │
   │   Serving!                                        │
   │                                                   │
   │   - Local:            http://localhost:9527       │
   │   - On Your Network:  http://192.168.2.102:9527   │
   │                                                   │
   └───────────────────────────────────────────────────┘

As you can see, the first line echo the string which preserved quotes; the second line direct execute sh -c "serve -l 9527 -s dist" by using eval. Note that, you can pass any command you want to eval as script1.sh's arguments.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
allenyllee
  • 964
  • 1
  • 13
  • 16
  • But ['eval' is evil](https://stackoverflow.com/questions/17529220/why-should-eval-be-avoided-in-bash-and-what-should-i-use-instead)(?). – Peter Mortensen Aug 21 '23 at 15:09
-2

Just separate each argument using quotes, and the nul character:

#! /bin/bash


sender () {
    printf '"%s"\0' "$@"
}


receiver () {
    readarray -d '' args < <(function "$@")
}


receiver "$@"

As commented by Charles Duffy.