Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:52:43+00:00 2026-06-12T08:52:43+00:00

array=(a b c d) I would like to add a character before each element

  • 0
array=(a b c d)

I would like to add a character before each element of the array in order to have this

array=(^a ^b ^c ^d)

An easy way to do that is to loop on array elements and change values one by one

for i in "${#array[@]}"
do
    array[i]="^"array[i]
done

But I would like to know if there is any way to do the same thing without looping on the array as I have to do the same instruction on all elements.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:52:44+00:00Added an answer on June 12, 2026 at 8:52 am

    Use Parameter Expansion:

    array=("${array[@]/#/^}")
    

    From the documentation:

    ${parameter/pattern/string}

    Pattern substitution. The pattern is expanded to produce a pattern just as in pathname
    expansion. Parameter is expanded and the longest match of pattern against its value is
    replaced with string. If pattern begins with /, all matches of pattern are replaced with
    string. Normally only the first match is replaced. If pattern begins with #, it must
    match at the beginning of the expanded value of parameter. If pattern begins with %, it
    must match at the end of the expanded value of parameter. If string is null, matches of
    pattern are deleted and the / following pattern may be omitted. If parameter is @ or *,
    the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the
    substitution operation is applied to each member of the array in turn, and the expansion is
    the resultant list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have json_encoded array that i would like to add to using php [{id:a,value:2},{id:b,value:2}]
I have a masked array that I would like to add to another array
I'm not sure how difficult this but I have an array and would like
I have an array that I would like to sort using a date field
I have an SHA-1 byte array that I would like to use in a
I have an array, I would like to add another value to it from
I would like to add a hash into an array using Ruby version 1.8.7:
I have an array which i would like to sort it based on values
I have an array which i would like to sort it based on values
I have a very large multidimensional array and I would like to sort it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.