Find the answer to your Linux question:
Results 1 to 3 of 3
I'm writing a bash script and I need to add a : (colon) before each of the elements of my array when I list the array elements. For the sake ...
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Posts
    2

    Manipulating array output/string manipulation

    I'm writing a bash script and I need to add a : (colon) before each of the elements of my array when I list the array elements. For the sake of being able to reuse the variables easily in other parts of my script, I don't want to add the colons to the array elements when I define the array. I also would prefer not to loop through the elements by getting a count, so as to keep it simple and would prefer to just use the * to get my elements.

    Code:
    #PATHS I WOULD LIKE TO USE
    MYPATHS=('/etc' '/home' '/var/www')
    
    echo ${MYPATHS[*]}
    Here is my desired result

    :/etc :/home :/var/www

    I'm sure this is easy, but it's driving me crazy. The smaller/simpler the command the better as others have to read and edit the script. All suggestions are welcome though. Thanks for the help.

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    If you don't mind ugliness, use this:

    Code:
     ${MYPATHS[*]/#/:}

  3. #3
    Just Joined!
    Join Date
    Nov 2006
    Posts
    2

    Thumbs up

    That's what I was thinking I would have to do. I would prefer to just add a character for simplicity sake, but replacement will have to do for now. Thanks.

Posting Permissions

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