sed a variable in bash


Results 1 to 3 of 3

Thread: sed a variable in bash

  1. #1
    Join Date
    Jan 2003
    Posts
    1,012

    sed a variable in bash

    I have beat this enough and don't get what should have been a very simple thing to do. I build a variable;

    Code:
    CLIST=java,lua,python,php,perl,ruby,tcl
    CLIST will be used by another bash script but I need to replace the commas with a space. I can pass the CLIST contents to this other script variable with no problem. When the other script is run it shows this;

    Code:
    list='java,lua,python,php,perl,ruby,tcl'
    But that script won't use them because of the commas. So I figured this should do the job at replacing those commas with a space (what the other script really wants);

    Code:
    CLIST="$(echo $CLIST | sed 's:,: :g')"
    In my mind that should have worked but it doesn't. The other script output get is;

    Code:
    list='java'
    I don't understand why everything after "java" gets cut off.
    You can tuna piano, but you can't tune a fish.

    http://www.lunar-linux.org/
    It's worth the spin.

    http://www.pclinuxos.com/page.php?7
    Puts the rest to shame.

  2. #2
    Join Date
    Sep 1999
    Location
    Cambridge, UK
    Posts
    509
    First of all there's no need to use sed because bash can do the replacement for you.
    Code:
    CLIST=${CLIST//,/ }
    Unfortunately you do not show how CLIST is passed to the second script so one can only guess what the problem is. My guess would be that somewhere along the way you are not quoting your arguments correctly. Perhaps you do this in script1
    Code:
    script2 $CLIST
    in which case script2 will receive one argument for each member of the list. The fix would be to call script2 as
    Code:
    script2 "$CLIST"
    If that isn't your problem, post the part of script1 which calls script2 and the part of script2 which interprets the list.

  3. #3
    Join Date
    Jan 2003
    Posts
    1,012
    I tried

    Code:
    CLIST=${CLIST//,/ }
    and that gave me the same results. Your comment about the other script got me to thinking. So the solution I came up with was to sed out the commas after the other script got the CLIST variables. Probably not the ideal method but it worked and good enough for now.

    Thanks for pointing me in another direction.
    You can tuna piano, but you can't tune a fish.

    http://www.lunar-linux.org/
    It's worth the spin.

    http://www.pclinuxos.com/page.php?7
    Puts the rest to shame.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •