how to add symbol in a string on bash

In a bash script i'm using command:

branch=git branch --contains 86cf181ec0df06656e57c7787866087592

output:

branch1 branch2 branch3

In next step i'm using command:

list_of_commits=$(git rev-list 86cf181ec0df06656e57c7787866087592 $branch --first-parent | tail -n 1)

but argument $branch must be written ^branch1 ^branch2 ^branch3 from $branch and full command:

list_of_commits=$(git rev-list 86cf181ec0df06656e57c7787866087592 ^branch1 ^branch2 ^branch3 --first-parent | tail -n 1)

How i can to add symbol ^ before every output words in argument $branch in my script?

Answers


This can make it:

$ d="hello i am muzzy"
$ echo $d | awk '{for (i=1;i<=NF; i++) {$i="^"$i} print}'
^hello ^i ^am ^muzzy

or if you want to save it in a new var:

$ new_d=$(echo $d | awk '{for (i=1;i<=NF; i++) {$i="^"$i} print}')
$ echo $new_d 
^hello ^i ^am ^muzzy

In your case,

branch=$(echo $branch | awk '{for (i=1;i<=NF; i++) {$i="^"$i} print}')

You could use arrays with special parameter expansions instead:

branch=($(git branch --contains 86cf181ec0df06656e57c7787866087592))
list_of_commits=$(git rev-list 86cf181ec0df06656e57c7787866087592 "${branch[@]/#/^}" --first-parent | tail -n 1)

Need Your Help

Prolog - Finding object with shortest inner List in a List

list variables recursion binding prolog

I have a large list BIGLIST that consists solely of any number of smaller lists SMALLLIST, which themselves contain another list INNERLIST in their first index. I need to design a predicate that ca...

Why is there huge performance hit in 2048x2048 versus 2047x2047 array multiplication?

c# arrays matrix-multiplication

I am making some matrix multiplication benchmarking, as previously mentioned in

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.