19
function foo() {
A=$@...
echo $A
}

foo bla "hello ppl"

I would like the output to be:
"bla" "hello ppl"

What do I need to do instead of the ellipsis?

3
  • What are you trying to accomplish? Is the output of foo to be used as input to anything or do you just want the raw text?
    – msw
    Jun 23, 2010 at 18:32
  • It's supposed to be used as input for another command (mail in this case).
    – rui
    Jun 23, 2010 at 18:34
  • 4
    @rubim: then I think you are making it much harder than it need be. mail "$@" would give the results without the un-parse / re-parse step. If "$@" isn't in the proper state, please show what you do have.
    – msw
    Jun 23, 2010 at 18:47

6 Answers 6

26

@msw has the right idea (up in the comments on the question). However, another idea to print arguments with quotes: use the implicit iteration of printf:

foo() { printf '"%s" ' "$@"; echo ""; }

foo bla "hello ppl"
# => "bla" "hello ppl"
8
  • 3
    Ding! You win the big prize. Giant teddy bear or four-foot beer-bottle coin bank? Jun 24, 2010 at 1:02
  • Why did you add echo ""; at the end? Isn't it unnecessary. Mar 13, 2015 at 20:21
  • So the output would end with a newline. Just so it looks nice and doesn't mess up your prompt. I realize it's unnecessary when called with a command substitution Mar 13, 2015 at 20:34
  • Thanks it works great. To bad there isn't a simple, one liner, bash version of this. Mar 13, 2015 at 20:42
  • 2
    uh, didn't I provide a "simple one liner"? Mar 13, 2015 at 20:49
6

Use parameter substitution to add " as prefix and suffix:

function foo() {
    A=("${@/#/\"}")
    A=("${A[@]/%/\"}")
    echo -e "${A[@]}"
}

foo bla "hello ppl" kkk 'ss ss'

Output

"bla" "hello ppl" "kkk" "ss ss"
3

You can use "$@" to treat each parameter as, well, a separate parameter, and then loop over each parameter:

function foo() {
for i in "$@"
do
    echo -n \"$i\"" "
done
echo
}

foo bla "hello ppl"
2
  • 1
    +1 This solution works! I am lazy, so I usually drop the 'in "$@"' part and only use 'for i'
    – Hai Vu
    Jun 23, 2010 at 18:25
  • I forgot to mention this in the question but I meant to do this without using a loop. Was looking for some magic bash expansion or something.
    – rui
    Jun 23, 2010 at 18:34
3

ninjalj had the right idea, but the use of quotes was odd, in part because what the OP is asking for is not really the best output format for many shell tasks. Actually, I can't figure out what the intended task is, but:

function quote_args {
   for i ; do
      echo \""$i"\"
   done
}

puts its quoted arguments one per line which is usually the best way to feed other programs. You do get output in a form you didn't ask for:

$ quote_args this is\ a "test really"
"this"
"is a"
"test really"

but it can be easily converted and this is the idiom that most shell invocations would prefer:

$ echo `quote_args this is\ a "test really"`
"this" "is a" "test really"

but unless it is going through another eval pass, the extra quotes will probably screw things up. That is, ls "is a file" will list the file is a file while

$ ls `quote_args is\ a\ file`

will try to list "is, a, and file" which you probably don't want.

2

No loop required:

foo() { local saveIFS=$IFS; local IFS='"'; local a="${*/#/ \"}"; echo "$a"\"; IFS=$saveIFS; }

Saving IFS isn't necessary in this simple example, especially restoring it right before the function exits, due to the fact that local is used. However, I included it in case other things are going into the function that a changed IFS might affect.

Example run:

$ foo a bcd "efg hij" k "lll mmm nnn " ooo "   ppp   qqq   " rrr\ sss
 "a" "bcd" "efg hij" "k" "lll mmm nnn " "ooo" "   ppp   qqq   " "rrr sss"
1

The only solution at this time that respects backslashes and quotes inside the argument:

$ concatenate() { printf "%q"" " "$@"; echo ""; }
$ concatenate arg1 "arg2" "weird arg3\\\\\\bbut\" legal!"
arg1 arg2 weird\ arg3\\\\\\bbut\"\ legal\!

Notice the "%q"" "

%q ARGUMENT is printed in a format that can be reused as shell input, escaping non-printable characters with the proposed POSIX $'' syntax.

Special characters (\, \b backspace, ...) will indeed be interpreted by the receiving program, even if not displayed interpreted in the terminal output.

Let's test:

# display.sh: Basic script to display the first 3 arguments passed
echo -e '#!/bin/bash'"\n"'echo -e "\$1=""$1"; echo -e "\$2=""$2"; echo -e "\$3=""$3"; sleep 2;' > display.sh
sudo chmod 755 display.sh

# Function to concatenate arguments as $ARGS
# and "evalutate" the command display.sh $ARGS
test_bash() { ARGS=$(concatenate "$@"); bash -c "./display.sh ""$ARGS"; }

# Test: Output is identical whatever the special characters
./display.sh arg1 arg2 arg3
test_bash    arg1 arg2 arg3

More complicate test:

./display.sh arg1 "arg2-1:Y\b-2:Y\\b-3:Y\\\b-4:Y\\\\b-5:Y\\\\\b-6:Y\\\\\\b" "arg3-XY\bZ-\"-1:\-2:\\-3:\\\-4:\\\\-5:\\\\\-6:\\\\\\-"
test_bash    arg1 "arg2-1:Y\b-2:Y\\b-3:Y\\\b-4:Y\\\\b-5:Y\\\\\b-6:Y\\\\\\b" "arg3-XY\bZ-\"-1:\-2:\\-3:\\\-4:\\\\-5:\\\\\-6:\\\\\\-"

In display.sh, we are using echo -e instead of just echo or printf in order to interpret the special characters. This is only representative if your called program interprets them.

-e enable interpretation of backslash escapes

If -e is in effect, the following sequences are recognized:

  • \ backslash
  • \a alert (BEL)
  • \b backspace
  • Etc.

NB: \b is the backspace character, so it erases Y in the example.

Note that this example is not to be reproduced in real code:

Thanks to the accepted answer and Danny Hong answer in "How to escape double quote inside a double quote?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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