Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export git log to variable as multiline string

Tags:

git

shell

When I execute this command

 git log 23237d...1a8815 --oneline --pretty=tformat:"%h %B"

I get something like

1a88151 commit1

8a544c0 commit2

b168aa9 commit3

But when I want to export this to some variable:

export LOG=`git log 23237d...1a8815 --oneline --pretty=tformat:"%h %B"`

And output it: echo $LOG, I get this:

1a88151 commit1 8a544c0 commit2 b168aa9 commit3

How can I make multiline export?

like image 878
Guseyn Ismayylov Avatar asked Dec 08 '22 12:12

Guseyn Ismayylov


2 Answers

You need to quote the expansion of the LOG variable in the call to echo:

echo "$LOG"

This prevents word splitting from taking place. You don't need to modify the IFS variable in this case.

like image 192
Colin D Bennett Avatar answered Dec 11 '22 09:12

Colin D Bennett


Bash processes the input using the contents of the IFS variable. From the docs:

The Internal Field Separator (IFS) that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>.

You can change the values of IFS to alter the behaviour:

IFS='' export LOG=`git log 23237d...1a8815 --oneline --pretty=tformat:"%h %B"`
like image 43
match Avatar answered Dec 11 '22 07:12

match