Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing command line arguments in quoted form

$@ when double quoted("$@") expands to "$1" "$2" "$3" …

There seems to be no easy way to print quoted form of what is inside "$@"

$ set -- "one two" three  
$ echo "$@"
one two three

What I am looking for is, output of

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

'one two' three

Using ‘set -x’, does echo quoted form of arguments inside "$@"

$ set -x; echo "$@"; set +x
+ set -x
+ echo 'one two' three
one two three
+ set +x

User defined variable $DOLLAR_AT contains same things inside it, as what
"$@" contains

$ DOLLAR_AT="'one two' three"

Dumping out, what is inside $DOLLAR_AT, using ‘echo’ works as expected

$ echo $DOLLAR_AT
'one two' three

due to some kind of automatic quoting by the shell of arguments that contain special characters

$ set -x; echo $DOLLAR_AT; set +x
+ echo ''\''one' 'two'\''' three
+ set +x

It is not clear why

$ echo "$@"

does not produce same output as

$ echo "$DOLLAR_AT"

When starting a command from a shell script using received "$@", there is a
need to print out what is inside "$@" in a quoted form before invoking the
command.

There is a similar need in printing out command line arguments passed from
a bash array.

$ CMDARGS=("one two" "three")

Evaluating the array within double quotes does not print array elements in a quoted form

$ echo "${CMDARGS[@]}"
one two three

I also need a compact way to print what is in $CMDARGS[@] in a quoted
form similar to what comes out of this iteration

$ for ARG in "${CMDARGS[@]}"; do echo \'$ARG\'; done
'one two'
'three'

>Solution :

You could try inline:

$ set -- 'foo bar' baz
$ echo ${@@Q}
'Foo bar' 'baz'

$ echo ${@@A}
set -- 'Foo bar' 'baz'

$ DOLLAR_AT="'one two' three"
$ echo ${DOLLAR_AT@Q}
''\''one two'\'' three'

$ echo ${DOLLAR_AT@A}
DOLLAR_AT=''\''one two'\'' three'

More info in bash’s manpage, under Parameter Expansion subsection.

   ${parameter@operator}
         Parameter transformation.  The expansion is either a transforma‐
         tion  of  the  value of parameter or information about parameter
         itself, depending on the value of operator.  Each operator is  a
         single letter:
 ...
         Q      The  expansion is a string that is the value of parameter
                quoted in a format that can be reused as input.
 ...
         A      The expansion is a string in the form  of  an  assignment
                statement  or  declare  command  that, if evaluated, will
                recreate parameter with its attributes and value.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading