r/shell • u/Badshah57 • Dec 14 '22
Slicing a string in shell script with delimitter |
Hi, I am working on a bourne shell script. And I'm stuck in one place.
I have a string for example, "1|name". I want one variable to take the value "1" and another variable to take value "name"
This can be solved easily with bash arrays.
But bourne shell doesn't have arrays.
Is there any way I can slice it with delimitter "|"?
Google search results were not helpful so here I'm.
1
Upvotes
7
u/Schreq Dec 14 '22
You can use the positional parameters:
1
u/[deleted] Dec 14 '22
Without arrays, I think if you want to put each part into a variable, you'll have to extract and assign each individually.
1
u/oh5nxo Dec 16 '22
It can be done piece-by-piece too, dodging a problem with '1|*'