4

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?

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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"`

2 Comments

The problem with this solution, setting IFS to the empty string, is that it's misleading. The IFS='' doesn't affect the line that calls 'git log' and stores it in the LOG variable, but instead IFS='' affects the echo $LOG line, and also persists for the rest of the shell session, with weird an unexpected effects.
Try the following in your shell and note which ones display y y y and which ones preserve the newlines and output three lines of y: (1) IFS=''; LOG=`yes | head -n3`; unset IFS; echo $LOG (2) unset IFS; LOG=`yes | head -n3`; IFS=''; echo $LOG (3) unset IFS; LOG=`yes | head -n3` ; echo "$LOG"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.