String concat that keeps quotes

Hi All,

I hope you can help. i am concatenating String variables using the following method.

command="$command$x"

i have created a script which takes a set of args passed to the script using the $*

#example script
args=$*
count=0
for x in $args 
do
    count=`expr $count + 1`
    if [ "$count" -gt "3" ] 
        then
            command="$command$x"
    fi
done

echo $command

this seems to work if i write

example this is a test to see if it works

it should output
test to see if it works

Now i am finally coming to my question
if i write

example this is a test to "see if" it works

it still prints out
test to see if it works
it always seems to remove the quotes from the string. how can i keep the quotation in the output

Many thanks for your help

The quotation marks are not passed in literally. Compare:

vnix$ echo foo
foo
vnix$ echo "foo"
foo
vnix$ echo '"foo"'
"foo"

If you change the loop so that it prints every item it processes, you should find that "see if" is handled as a single argument.
See further UNIX Shell Quotes - a simple tutorial

As an aside, note that "$@" should be preferred over $*

Hi era,

I have noticed that
echo $@
or
echo $*

seem to remove the quotes from the arguments in the scripts. Is there a away to keep this

i.e example script
example this is an "example" test
echo $* or $@
echo's
this is an "example" test

rather than

this is an example test

If you want to keep the quotes you can escape them with a slash when writing the parameter like

./mach.ksh \"example\" test
$@: "example" test
$*: "example" test

Read the grymoire.com quoting tutorial from the link above. Quote characters are inherently special; you can't get literal quotes through in the shell without additional quoting or other escaping mechanisms.

thanks era, i have been through the link you provide UNIX Shell Quote
this make perfect sense, unfortunately I am in big trouble i am using an application which allows me access to the arguments i cant make changes to the original commands that are passed,
so i cant wrap them round / or '

i can just use these arguments to create scripts which i use.

The problems is some of the commands that can be passed have quotations which seem to be disappear

so i assume there is no walk around this?

Many thanks

Can you do something like

tr '"' '~'
your stuff, then
tr '~' '"'

Doing your own parsing of the arguments is usually not a good idea. Is there a particular reason you want to loop over the arguments and collect them into a single string? If keeping quoting etc. intact is important, the "$@" construct is exactly for that, but then e.g. editing out some of the command-line arguments and passing on the rest is tricky. (Shifting off the first argument is of course always simple and straightforward.)

Hi era,

If i have something like ./example.sh does "this" work

If I echo "$@" then this outputs. does this work

i have tried using the shift but that's give the same problem because the $@ has already removed the quotes. Is there anyway of getting exactly what was inputted in from the above example without actually modifying the command parameter with escape characters.

Many thanks for your help