How to "serialize" and "deserialize" command line arguments to string in bash?

Posted by Vi on Stack Overflow See other posts from Stack Overflow or by Vi
Published on 2010-06-18T11:22:15Z Indexed on 2010/06/18 11:43 UTC
Read the original article Hit count: 256

Filed under:
|
|

I call my script:

$ ./script 'a!#*`*&
^$' "sdf sdf\"qw sdsdf" 1 -- 2 3

It gets arguments:

1: a!#*`*&
^$
2: sdf sdf"qw sdsdf
3: 1
4: --
5: 2
6: 3

If I need to call something with the same arguments locally, I do this:

someprogram "$@"

But how can I put all that array to a string (to store in file or in environment variable or pass over TCP eaisly) and then turn it back to command line arguments somewhere? I want it to be simple, short and secure.

export CMDLINE="$@"
# What is in CMDLINE now? Escaped or not?

sh -c "someprogram $CMDLINE"
# Will it do what I mean?

Ideally I want two bash subroutines: the first turns turns any Bash array into a [a-zA-Z0-9_]* string, the other turns it back to Bash array I can use.

© Stack Overflow or respective owner

Related posts about bash

Related posts about serialization