2

I want to construct array element with space, it works while run in command line:

pc:/tmp$ b=('1 2' '3 4')
pc:/tmp$ echo $b
1 2
pc:/tmp$ echo ${b[1]}
3 4

But it get the elements split with space in script:

pc:/tmp$ cat t.sh
a="'1 2' '3 4'"
echo $a
b=($a)
echo $b
echo ${b[1]}
echo ${b[2]}

pc:/tmp$ bash t.sh
'1 2' '3 4'
'1
2'
'3

I want to get '1 2' with echo $b as get '3 4' with ${b[1]} in script.

How could I achieve this?

And why it can't not works in script but works in command line?

4
  • 3
    b=($a) splits the string on white space. You would have to write a parser for your a to tear it apart. Commented Sep 27, 2021 at 14:19
  • If you tested b=($a) at the command line it would behave just the same way as it did in your script. This isn't a script-vs-command-line difference. Commented Sep 27, 2021 at 15:32
  • BTW, among the answers on the duplicates, I strongly suggest finding one that uses either the Python shlex module (if you want the closest possible accuracy to a real POSIX sh parser), or xargs printf '%s\0' (if you want something that's available everywhere and doesn't add more bugs than the minimum set xargs forces). Commented Sep 27, 2021 at 15:35
  • @CharlesDuffy THX, thanks for your time, maybe I should improve my search skill, that all relevant questions are helpful. Commented Sep 27, 2021 at 16:18

1 Answer 1

4

The code a="'1 2' '3 4'" does not create an array, it creates a string with embedded single-quotes.

$: a="'1 2' '3 4'"
$: echo "[$a]"
['1 2' '3 4']

If you want to create an array, you have to use the right syntax.

$: a=( '1 2' '3 4' )
$: printf "[%s]\n" "${a[@]}"
[1 2]
[3 4]

Then if you want to copy it to another array, just treat it like an array instead of a string.

b=( "${a[@]}" )
printf "[%s]\n" "${b[@]}"
[1 2]
[3 4]

To convert the string created in a="'1 2' '3 4'" to an array is a little trickier.

IF you know that the format is a consistent match for the above, try this:

$: echo "[$a]"
['1 2' '3 4']
$: IFS='#' read -ra b < <( sed -E "s/' +'/#/g; s/ *' *//g" <<< "$a" )
$: printf "[%s]\n" "${b[@]}"
[1 2]
[3 4]

but generally, this is a bad plan. Try to fix your input!

Sign up to request clarification or add additional context in comments.

4 Comments

I means I have a string variable a="'1 2' '3 4'", and how could I translate it to a array? THX.
That's a little harder. How consistent is that format?
We already have existing knowledge base entries that do what the OP is asking for.
@PaulHodges THX, the pattern is consistent with mine, it can solve my problem, THX.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.