4

Premise

I realise this question already exists, but the solutions are not actually changing the delimiter. I would like to know if there is anyway to change the delimiter or if anybody knows where it resides.


Example

Say i need to pass this string to a program, delimited by commas as that is what the program accepts

echo \"abc" "{def,ghi}\"

prints

"abc def" "abc ghi"

where i would want

"abc def","abc ghi"

Obviously this is just a simple example though.


What i have tried

Nothing as i have no idea where to look for this delimiter, although i have searched pretty extensively

Also as a sub question:

Using choroba's answer from the other question creates errors when trying to use it to pass it to my script. Admittedly i haven't tried for long though

I tried

./script ( set abc" "{def,ghi} ; IFS=: ; echo "$*" )
./script < <( set abc" "{def,ghi} ; IFS=: ; echo "$*" )
./scipt $(( set abc" "{def,ghi} ; IFS=: ; echo "$*" ))
5
  • Using printf you can say printf "%s," \"abc" "{def,ghi}\" and you'll get "abc def","abc ghi",. But this is just an idea.
    – fedorqui
    Jun 17, 2015 at 12:42
  • 1
    This worked for me: (set \"abc\ {def,ghi}\"; IFS=,; echo "$*") prints: "abc def","abc ghi".
    – RedX
    Jun 17, 2015 at 12:44
  • @fedorqui Unfortunately that leaves a trailing comma :(
    – 123
    Jun 17, 2015 at 12:45
  • @RedX Please read the question.
    – 123
    Jun 17, 2015 at 12:45
  • @fedorqui Did you mean to comment on this ? I'm not storing any commands, nor using any variables. I just want to be able to change the delimiter in brace expansion so i can use it with my custom script, preferably in a way that allows embedded spaces.
    – 123
    Jun 17, 2015 at 13:56

1 Answer 1

6

So my test script looks like:

#!/usr/bin/env bash

echo "$@"

And it works fine with:

./my_script.sh $(set \"abc\ {def,ghi}\"; IFS=,; echo "$*")

It outputs:

"abc def","abc ghi"

I guess you were missing the $ to invoke the subshell.

Updated for main question (whether there is an option to change the separator):

After looking into bash v4.3 source:

When doing

./my_script.sh \"abc\ {1,2}\"

there is no way of adding any other separator because there is no separator. The command gets two arguments passed to it "abc 1" and "abc 2".

The case of

echo \"abc\ {1,2}\"

You would need to expand the built-in echo to have either an option or global variable to set the words separator. Currently this is done by putchar(' ') in echo_builtin in builtins/echo.def:192

3
  • I'm gonna upvote this because it answered the sub question but my main question still needs answering. I probably shouldn't have added that at the end, my fault.
    – 123
    Jun 17, 2015 at 12:53
  • 1
    @User112638726 Ok I though, the problem below was exactly the use case for your problem. If your use-case is a different one, post that and maybe we can solve that too. I will look into the source-code of bash to see if there is another way of solving this.
    – RedX
    Jun 17, 2015 at 12:56
  • I'm also looking through to see if i can find anything, If i do I'll let you know :)
    – 123
    Jun 17, 2015 at 13:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.