How to "serialize" and "deserialize" command line arguments to string in bash?
- by Vi
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.