Bash, command to variable (cat), output is on 1 line, is not workingHow can I delete a file if it starts with <html> in bash?Disappearing files in the cat commandStore grive command output to variable in bash scriptbash send output from command to variableNumbering paragraphs with the 'cat' commandBash with cat for automatic merging pair of filesGiving cat output to rmCat command with variable in file name not working

What is the mathematical formula for proficiency bonus vs level/CR?

Biggest Irreducible Hello World

How should I analyze this passage? As vii°7, V7, or both?

Why do some AFBs have planes parked at 45 degrees to others?

What's the difference between xxxx-client and xxxx-server packages?

Writing Vec compare in a more compact way

How to say No to idea given by team member, when I know from my experience that it is going to fail?

Team lunch vs food preferences: should I skip team lunch? request change of venue? something else? (vege food prefs the venue can't accommodate)

Why is JavaScript not compiled to bytecode before sending over the network?

How to teach children Santa is not real, while respecting other kids beliefs?

Home rebuild and demolish

Why didn't the 6502 have increase/decrease opcodes for A?

Smallest Fibonacci Multiples

Why doesn't apt detect the newer .deb version in the universe repository, even though it exists there?

Why did Bayer lose aspirin and heroin trademarks under the 1919 Treaty of Versailles?

How can I line up an equation with the equals sign from another equation?

Who created Avada Kedavra?

Python __getitem__ and in operator result in strange behavior

When is it not okay to cheap out on bike parts?

Fourier transform is an isomorphism...but we don’t get when each frequency appears?

What's the meaning of "411 on the late-night drop box"?

How do I send a Ctrl+Alt+Delete when connected from the Screen Sharing app to a Windows computer?

Conversion of mass into energy with 100% efficiency

Confused about Autoregressive AR(1) process



Bash, command to variable (cat), output is on 1 line,


 is not working

How can I delete a file if it starts with <html> in bash?Disappearing files in the cat commandStore grive command output to variable in bash scriptbash send output from command to variableNumbering paragraphs with the 'cat' commandBash with cat for automatic merging pair of filesGiving cat output to rmCat command with variable in file name not working



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









2


















Issue



In bash, output is on 1 line from command, so <pre> is not working in html tag.



log=`cat ../local/log/$(ls -t ../local/log | head -1 )`
echo '
<tr style="background-color: rgb(250,240,240);">
<td style="padding: 10px; text-align: center;"><pre>'$log'</pre></td>
</tr>
' > itf-check-article.html


How can I do get output to variable as classic line by line, not in one line?



Thanks.



Update.



Command.



log=`cat ../local/log/$(ls -t ../local/log | head -1 )`; echo $log.


Output (1 line).



Start of processing orders. 0 order(s) in this run. No orders in this run


But this command shows me this (with 2 lines).



itfstage@vm14258:~/sofimon$ cat ../local/log/UNIHOBBY_ITF_ORDERS.log
Start of processing orders. 0 order(s) in this run.
No orders in this run
itfstage@vm14258:~/sofimon$









share|improve this question



























  • What is the usage of the first line? Can you put the output of echo "$log" after the 1st line is executed in your question.

    – FedonKadifeli
    Sep 30 at 9:00











  • You question is not clear. But, I think you need double quotes around $log like this: ...<pre>'"$log"'</pre>

    – FedonKadifeli
    Sep 30 at 9:16













2


















Issue



In bash, output is on 1 line from command, so <pre> is not working in html tag.



log=`cat ../local/log/$(ls -t ../local/log | head -1 )`
echo '
<tr style="background-color: rgb(250,240,240);">
<td style="padding: 10px; text-align: center;"><pre>'$log'</pre></td>
</tr>
' > itf-check-article.html


How can I do get output to variable as classic line by line, not in one line?



Thanks.



Update.



Command.



log=`cat ../local/log/$(ls -t ../local/log | head -1 )`; echo $log.


Output (1 line).



Start of processing orders. 0 order(s) in this run. No orders in this run


But this command shows me this (with 2 lines).



itfstage@vm14258:~/sofimon$ cat ../local/log/UNIHOBBY_ITF_ORDERS.log
Start of processing orders. 0 order(s) in this run.
No orders in this run
itfstage@vm14258:~/sofimon$









share|improve this question



























  • What is the usage of the first line? Can you put the output of echo "$log" after the 1st line is executed in your question.

    – FedonKadifeli
    Sep 30 at 9:00











  • You question is not clear. But, I think you need double quotes around $log like this: ...<pre>'"$log"'</pre>

    – FedonKadifeli
    Sep 30 at 9:16











2











2







2




Issue



In bash, output is on 1 line from command, so <pre> is not working in html tag.



log=`cat ../local/log/$(ls -t ../local/log | head -1 )`
echo '
<tr style="background-color: rgb(250,240,240);">
<td style="padding: 10px; text-align: center;"><pre>'$log'</pre></td>
</tr>
' > itf-check-article.html


How can I do get output to variable as classic line by line, not in one line?



Thanks.



Update.



Command.



log=`cat ../local/log/$(ls -t ../local/log | head -1 )`; echo $log.


Output (1 line).



Start of processing orders. 0 order(s) in this run. No orders in this run


But this command shows me this (with 2 lines).



itfstage@vm14258:~/sofimon$ cat ../local/log/UNIHOBBY_ITF_ORDERS.log
Start of processing orders. 0 order(s) in this run.
No orders in this run
itfstage@vm14258:~/sofimon$









share|improve this question














Issue



In bash, output is on 1 line from command, so <pre> is not working in html tag.



log=`cat ../local/log/$(ls -t ../local/log | head -1 )`
echo '
<tr style="background-color: rgb(250,240,240);">
<td style="padding: 10px; text-align: center;"><pre>'$log'</pre></td>
</tr>
' > itf-check-article.html


How can I do get output to variable as classic line by line, not in one line?



Thanks.



Update.



Command.



log=`cat ../local/log/$(ls -t ../local/log | head -1 )`; echo $log.


Output (1 line).



Start of processing orders. 0 order(s) in this run. No orders in this run


But this command shows me this (with 2 lines).



itfstage@vm14258:~/sofimon$ cat ../local/log/UNIHOBBY_ITF_ORDERS.log
Start of processing orders. 0 order(s) in this run.
No orders in this run
itfstage@vm14258:~/sofimon$


bash cat




share|improve this question













share|improve this question











share|improve this question


share|improve this question


edited Sep 30 at 9:16



genderbee









asked Sep 30 at 8:39



genderbeegenderbee

5429 bronze badges


5429 bronze badges











  • What is the usage of the first line? Can you put the output of echo "$log" after the 1st line is executed in your question.

    – FedonKadifeli
    Sep 30 at 9:00











  • You question is not clear. But, I think you need double quotes around $log like this: ...<pre>'"$log"'</pre>

    – FedonKadifeli
    Sep 30 at 9:16















  • What is the usage of the first line? Can you put the output of echo "$log" after the 1st line is executed in your question.

    – FedonKadifeli
    Sep 30 at 9:00











  • You question is not clear. But, I think you need double quotes around $log like this: ...<pre>'"$log"'</pre>

    – FedonKadifeli
    Sep 30 at 9:16










What is the usage of the first line? Can you put the output of echo "$log" after the 1st line is executed in your question.

– FedonKadifeli
Sep 30 at 9:00



What is the usage of the first line? Can you put the output of echo "$log" after the 1st line is executed in your question.

– FedonKadifeli
Sep 30 at 9:00







You question is not clear. But, I think you need double quotes around $log like this: ...<pre>'"$log"'</pre>

– FedonKadifeli
Sep 30 at 9:16



You question is not clear. But, I think you need double quotes around $log like this: ...<pre>'"$log"'</pre>

– FedonKadifeli
Sep 30 at 9:16






3 Answers
3






active

oldest

votes


















2



















When a Bash variable contains multiple lines and you want to output it with echo as it is; you need to put double quotes (") around the variable. For example:



echo '
.......<pre>
'"$log"'
</pre>...
' > ...


If you put double quotes, the echo command will consider it as a single (whose contents happen to contain a multi-line string) parameter. If you don't put double quotes the contents of the string will be split (by Bash) into multiple parameters whenever a white-space (space, tab, newline, etc.) occurs.



For example:



$ var='a b c'
$ echo $var
a b c
$ echo "$var"
a b c


Note, the first echo will get three parameters ('a', 'b', and 'c') and will output them by separating with a single space. The second echo will get a single parameter ('a b c') and will output it as it is.






share|improve this answer



























  • Yes! You are right. What is difference if I don't use "? Thanks.

    – genderbee
    Sep 30 at 9:21


















0



















Your pre-tag is working fine. Your problem is that $log contains the output of cat on one line. Instead of trying to fix this, I would suggest you split this code into simpler steps.



First of all, this line:



 log=`cat ../local/log/$(ls -t ../local/log | head -1 )`


Is way to complicated because it nests two command substitutions with two different syntaxes. You might understand it today, but why not split this into two steps so you or one of your co-workers will understand it a year from now:



 logfile=../local/log/$(ls -t ../local/log | head -1)
logtext=$(cat $logfile)


Or even better (assuming the logfiles end in .log):



 logfile=$(ls -t ../local/log/*.log | head -1)
logtext=$(cat $logfile)


Now you see that you just put the contents of $logile into $logtext. You seem to want to output the content of $logfile between the pre-tags, just like it appears in the file. With the simplified commands above, it may appear obvious that putting it into $logtext is an extra step that isn't even needed. Just cat the file where you want it to appear. The backticks or the $()-construct is meant to feed one command with arguments that are created from the output of another. In this case that just messes it all up.



So what you could do is this:



 outputfile=itf-check-article.html
logfile=$(ls -t ../local/log/*.log | head -1)
echo '<tr style="background-color: rgb(250,240,240);">' > $outputfile
echo '<td style="padding: 10px; text-align: center;"><pre>' >> $outputfile
cat $logfile >> $outputfile
echo '</pre></td></tr>' >> $outputfile


or simply run the commands between brackets () to start a subshell with one redirect statement instead:



 outputfile=itf-check-article.html
logfile=$(ls -t ../local/log/*.log | head -1)

(
echo '<tr style="background-color: rgb(250,240,240);">'
echo '<td style="padding: 10px; text-align: center;"><pre>'
cat $logfile
echo '</pre></td></tr>'
) > $outputfile





share|improve this answer

























  • Ok, maybe it is better to have it separately. But what is difference between 'command' and $(command) and when it is better use one method and when second method. Thanks.

    – genderbee
    Sep 30 at 11:50












  • @genderbee, there is no difference, except you can't nest these backticks. However you ou could easily do echo $(echo $(echo $(echo Bazinga!))) if you enjoy spawning three subshells just for fun.

    – Oscar
    Sep 30 at 12:12












  • Bazinga......! :)

    – genderbee
    Sep 30 at 12:14











  • @genderbee, as for the second part of your question: it depends on how you define "better". Often "easier to understand for the human reading it" counts as better than "clever demonstration of all your knowledge packed into one complex statement"

    – Oscar
    Sep 30 at 12:15



















0



















special-purpose code block (here doc).



#!/bin/bash

cat > itf-check-article.html <<PRE
<tr style="background-color: rgb(250,240,240);">
<td style="padding: 10px; text-align: center;"><pre>$(awk 'printf "%s<br/>",$0' $(ls -td ../local/log/* | head -n 1))</pre></td>
</tr>
PRE


output:



$ cat itf-check-article.html
<tr style="background-color: rgb(250,240,240);">
<td style="padding: 10px; text-align: center;"><pre>Start of processing orders. 0 order(s) in this run.<br/>No orders in this run<br/></pre></td>
</tr>


Using += operator to append to variable.



#!/bin/bash

declare html

html+='<tr style="background-color: rgb(250,240,240);">n'
html+='<td style="padding: 10px; text-align: center;"><pre>'
html+=$(awk 'printf "%s<br/>",$0' $(ls -td ../local/log/* | head -n 1))
html+='</pre></td>n'
html+='</tr>'

echo -e "$html" > itf-check-article.html





share|improve this answer




























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1177616%2fbash-command-to-variable-cat-output-is-on-1-line-pre-is-not-working%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown
























    3 Answers
    3






    active

    oldest

    votes






    3 Answers
    3






    active

    oldest

    votes







    active

    oldest

    votes




    active

    oldest

    votes







    2



















    When a Bash variable contains multiple lines and you want to output it with echo as it is; you need to put double quotes (") around the variable. For example:



    echo '
    .......<pre>
    '"$log"'
    </pre>...
    ' > ...


    If you put double quotes, the echo command will consider it as a single (whose contents happen to contain a multi-line string) parameter. If you don't put double quotes the contents of the string will be split (by Bash) into multiple parameters whenever a white-space (space, tab, newline, etc.) occurs.



    For example:



    $ var='a b c'
    $ echo $var
    a b c
    $ echo "$var"
    a b c


    Note, the first echo will get three parameters ('a', 'b', and 'c') and will output them by separating with a single space. The second echo will get a single parameter ('a b c') and will output it as it is.






    share|improve this answer



























    • Yes! You are right. What is difference if I don't use "? Thanks.

      – genderbee
      Sep 30 at 9:21













    2



















    When a Bash variable contains multiple lines and you want to output it with echo as it is; you need to put double quotes (") around the variable. For example:



    echo '
    .......<pre>
    '"$log"'
    </pre>...
    ' > ...


    If you put double quotes, the echo command will consider it as a single (whose contents happen to contain a multi-line string) parameter. If you don't put double quotes the contents of the string will be split (by Bash) into multiple parameters whenever a white-space (space, tab, newline, etc.) occurs.



    For example:



    $ var='a b c'
    $ echo $var
    a b c
    $ echo "$var"
    a b c


    Note, the first echo will get three parameters ('a', 'b', and 'c') and will output them by separating with a single space. The second echo will get a single parameter ('a b c') and will output it as it is.






    share|improve this answer



























    • Yes! You are right. What is difference if I don't use "? Thanks.

      – genderbee
      Sep 30 at 9:21











    2













    2









    2





    When a Bash variable contains multiple lines and you want to output it with echo as it is; you need to put double quotes (") around the variable. For example:



    echo '
    .......<pre>
    '"$log"'
    </pre>...
    ' > ...


    If you put double quotes, the echo command will consider it as a single (whose contents happen to contain a multi-line string) parameter. If you don't put double quotes the contents of the string will be split (by Bash) into multiple parameters whenever a white-space (space, tab, newline, etc.) occurs.



    For example:



    $ var='a b c'
    $ echo $var
    a b c
    $ echo "$var"
    a b c


    Note, the first echo will get three parameters ('a', 'b', and 'c') and will output them by separating with a single space. The second echo will get a single parameter ('a b c') and will output it as it is.






    share|improve this answer














    When a Bash variable contains multiple lines and you want to output it with echo as it is; you need to put double quotes (") around the variable. For example:



    echo '
    .......<pre>
    '"$log"'
    </pre>...
    ' > ...


    If you put double quotes, the echo command will consider it as a single (whose contents happen to contain a multi-line string) parameter. If you don't put double quotes the contents of the string will be split (by Bash) into multiple parameters whenever a white-space (space, tab, newline, etc.) occurs.



    For example:



    $ var='a b c'
    $ echo $var
    a b c
    $ echo "$var"
    a b c


    Note, the first echo will get three parameters ('a', 'b', and 'c') and will output them by separating with a single space. The second echo will get a single parameter ('a b c') and will output it as it is.





    share|improve this answer













    share|improve this answer


    share|improve this answer


    edited Sep 30 at 9:26













    answered Sep 30 at 9:20



    FedonKadifeliFedonKadifeli

    1,6391 gold badge7 silver badges24 bronze badges


    1,6391 gold badge7 silver badges24 bronze badges











    • Yes! You are right. What is difference if I don't use "? Thanks.

      – genderbee
      Sep 30 at 9:21















    • Yes! You are right. What is difference if I don't use "? Thanks.

      – genderbee
      Sep 30 at 9:21










    Yes! You are right. What is difference if I don't use "? Thanks.

    – genderbee
    Sep 30 at 9:21



    Yes! You are right. What is difference if I don't use "? Thanks.

    – genderbee
    Sep 30 at 9:21







    0



















    Your pre-tag is working fine. Your problem is that $log contains the output of cat on one line. Instead of trying to fix this, I would suggest you split this code into simpler steps.



    First of all, this line:



     log=`cat ../local/log/$(ls -t ../local/log | head -1 )`


    Is way to complicated because it nests two command substitutions with two different syntaxes. You might understand it today, but why not split this into two steps so you or one of your co-workers will understand it a year from now:



     logfile=../local/log/$(ls -t ../local/log | head -1)
    logtext=$(cat $logfile)


    Or even better (assuming the logfiles end in .log):



     logfile=$(ls -t ../local/log/*.log | head -1)
    logtext=$(cat $logfile)


    Now you see that you just put the contents of $logile into $logtext. You seem to want to output the content of $logfile between the pre-tags, just like it appears in the file. With the simplified commands above, it may appear obvious that putting it into $logtext is an extra step that isn't even needed. Just cat the file where you want it to appear. The backticks or the $()-construct is meant to feed one command with arguments that are created from the output of another. In this case that just messes it all up.



    So what you could do is this:



     outputfile=itf-check-article.html
    logfile=$(ls -t ../local/log/*.log | head -1)
    echo '<tr style="background-color: rgb(250,240,240);">' > $outputfile
    echo '<td style="padding: 10px; text-align: center;"><pre>' >> $outputfile
    cat $logfile >> $outputfile
    echo '</pre></td></tr>' >> $outputfile


    or simply run the commands between brackets () to start a subshell with one redirect statement instead:



     outputfile=itf-check-article.html
    logfile=$(ls -t ../local/log/*.log | head -1)

    (
    echo '<tr style="background-color: rgb(250,240,240);">'
    echo '<td style="padding: 10px; text-align: center;"><pre>'
    cat $logfile
    echo '</pre></td></tr>'
    ) > $outputfile





    share|improve this answer

























    • Ok, maybe it is better to have it separately. But what is difference between 'command' and $(command) and when it is better use one method and when second method. Thanks.

      – genderbee
      Sep 30 at 11:50












    • @genderbee, there is no difference, except you can't nest these backticks. However you ou could easily do echo $(echo $(echo $(echo Bazinga!))) if you enjoy spawning three subshells just for fun.

      – Oscar
      Sep 30 at 12:12












    • Bazinga......! :)

      – genderbee
      Sep 30 at 12:14











    • @genderbee, as for the second part of your question: it depends on how you define "better". Often "easier to understand for the human reading it" counts as better than "clever demonstration of all your knowledge packed into one complex statement"

      – Oscar
      Sep 30 at 12:15














    0



















    Your pre-tag is working fine. Your problem is that $log contains the output of cat on one line. Instead of trying to fix this, I would suggest you split this code into simpler steps.



    First of all, this line:



     log=`cat ../local/log/$(ls -t ../local/log | head -1 )`


    Is way to complicated because it nests two command substitutions with two different syntaxes. You might understand it today, but why not split this into two steps so you or one of your co-workers will understand it a year from now:



     logfile=../local/log/$(ls -t ../local/log | head -1)
    logtext=$(cat $logfile)


    Or even better (assuming the logfiles end in .log):



     logfile=$(ls -t ../local/log/*.log | head -1)
    logtext=$(cat $logfile)


    Now you see that you just put the contents of $logile into $logtext. You seem to want to output the content of $logfile between the pre-tags, just like it appears in the file. With the simplified commands above, it may appear obvious that putting it into $logtext is an extra step that isn't even needed. Just cat the file where you want it to appear. The backticks or the $()-construct is meant to feed one command with arguments that are created from the output of another. In this case that just messes it all up.



    So what you could do is this:



     outputfile=itf-check-article.html
    logfile=$(ls -t ../local/log/*.log | head -1)
    echo '<tr style="background-color: rgb(250,240,240);">' > $outputfile
    echo '<td style="padding: 10px; text-align: center;"><pre>' >> $outputfile
    cat $logfile >> $outputfile
    echo '</pre></td></tr>' >> $outputfile


    or simply run the commands between brackets () to start a subshell with one redirect statement instead:



     outputfile=itf-check-article.html
    logfile=$(ls -t ../local/log/*.log | head -1)

    (
    echo '<tr style="background-color: rgb(250,240,240);">'
    echo '<td style="padding: 10px; text-align: center;"><pre>'
    cat $logfile
    echo '</pre></td></tr>'
    ) > $outputfile





    share|improve this answer

























    • Ok, maybe it is better to have it separately. But what is difference between 'command' and $(command) and when it is better use one method and when second method. Thanks.

      – genderbee
      Sep 30 at 11:50












    • @genderbee, there is no difference, except you can't nest these backticks. However you ou could easily do echo $(echo $(echo $(echo Bazinga!))) if you enjoy spawning three subshells just for fun.

      – Oscar
      Sep 30 at 12:12












    • Bazinga......! :)

      – genderbee
      Sep 30 at 12:14











    • @genderbee, as for the second part of your question: it depends on how you define "better". Often "easier to understand for the human reading it" counts as better than "clever demonstration of all your knowledge packed into one complex statement"

      – Oscar
      Sep 30 at 12:15












    0













    0









    0





    Your pre-tag is working fine. Your problem is that $log contains the output of cat on one line. Instead of trying to fix this, I would suggest you split this code into simpler steps.



    First of all, this line:



     log=`cat ../local/log/$(ls -t ../local/log | head -1 )`


    Is way to complicated because it nests two command substitutions with two different syntaxes. You might understand it today, but why not split this into two steps so you or one of your co-workers will understand it a year from now:



     logfile=../local/log/$(ls -t ../local/log | head -1)
    logtext=$(cat $logfile)


    Or even better (assuming the logfiles end in .log):



     logfile=$(ls -t ../local/log/*.log | head -1)
    logtext=$(cat $logfile)


    Now you see that you just put the contents of $logile into $logtext. You seem to want to output the content of $logfile between the pre-tags, just like it appears in the file. With the simplified commands above, it may appear obvious that putting it into $logtext is an extra step that isn't even needed. Just cat the file where you want it to appear. The backticks or the $()-construct is meant to feed one command with arguments that are created from the output of another. In this case that just messes it all up.



    So what you could do is this:



     outputfile=itf-check-article.html
    logfile=$(ls -t ../local/log/*.log | head -1)
    echo '<tr style="background-color: rgb(250,240,240);">' > $outputfile
    echo '<td style="padding: 10px; text-align: center;"><pre>' >> $outputfile
    cat $logfile >> $outputfile
    echo '</pre></td></tr>' >> $outputfile


    or simply run the commands between brackets () to start a subshell with one redirect statement instead:



     outputfile=itf-check-article.html
    logfile=$(ls -t ../local/log/*.log | head -1)

    (
    echo '<tr style="background-color: rgb(250,240,240);">'
    echo '<td style="padding: 10px; text-align: center;"><pre>'
    cat $logfile
    echo '</pre></td></tr>'
    ) > $outputfile





    share|improve this answer












    Your pre-tag is working fine. Your problem is that $log contains the output of cat on one line. Instead of trying to fix this, I would suggest you split this code into simpler steps.



    First of all, this line:



     log=`cat ../local/log/$(ls -t ../local/log | head -1 )`


    Is way to complicated because it nests two command substitutions with two different syntaxes. You might understand it today, but why not split this into two steps so you or one of your co-workers will understand it a year from now:



     logfile=../local/log/$(ls -t ../local/log | head -1)
    logtext=$(cat $logfile)


    Or even better (assuming the logfiles end in .log):



     logfile=$(ls -t ../local/log/*.log | head -1)
    logtext=$(cat $logfile)


    Now you see that you just put the contents of $logile into $logtext. You seem to want to output the content of $logfile between the pre-tags, just like it appears in the file. With the simplified commands above, it may appear obvious that putting it into $logtext is an extra step that isn't even needed. Just cat the file where you want it to appear. The backticks or the $()-construct is meant to feed one command with arguments that are created from the output of another. In this case that just messes it all up.



    So what you could do is this:



     outputfile=itf-check-article.html
    logfile=$(ls -t ../local/log/*.log | head -1)
    echo '<tr style="background-color: rgb(250,240,240);">' > $outputfile
    echo '<td style="padding: 10px; text-align: center;"><pre>' >> $outputfile
    cat $logfile >> $outputfile
    echo '</pre></td></tr>' >> $outputfile


    or simply run the commands between brackets () to start a subshell with one redirect statement instead:



     outputfile=itf-check-article.html
    logfile=$(ls -t ../local/log/*.log | head -1)

    (
    echo '<tr style="background-color: rgb(250,240,240);">'
    echo '<td style="padding: 10px; text-align: center;"><pre>'
    cat $logfile
    echo '</pre></td></tr>'
    ) > $outputfile




    share|improve this answer











    share|improve this answer


    share|improve this answer




    answered Sep 30 at 11:10



    OscarOscar

    1628 bronze badges


    1628 bronze badges











    • Ok, maybe it is better to have it separately. But what is difference between 'command' and $(command) and when it is better use one method and when second method. Thanks.

      – genderbee
      Sep 30 at 11:50












    • @genderbee, there is no difference, except you can't nest these backticks. However you ou could easily do echo $(echo $(echo $(echo Bazinga!))) if you enjoy spawning three subshells just for fun.

      – Oscar
      Sep 30 at 12:12












    • Bazinga......! :)

      – genderbee
      Sep 30 at 12:14











    • @genderbee, as for the second part of your question: it depends on how you define "better". Often "easier to understand for the human reading it" counts as better than "clever demonstration of all your knowledge packed into one complex statement"

      – Oscar
      Sep 30 at 12:15
















    • Ok, maybe it is better to have it separately. But what is difference between 'command' and $(command) and when it is better use one method and when second method. Thanks.

      – genderbee
      Sep 30 at 11:50












    • @genderbee, there is no difference, except you can't nest these backticks. However you ou could easily do echo $(echo $(echo $(echo Bazinga!))) if you enjoy spawning three subshells just for fun.

      – Oscar
      Sep 30 at 12:12












    • Bazinga......! :)

      – genderbee
      Sep 30 at 12:14











    • @genderbee, as for the second part of your question: it depends on how you define "better". Often "easier to understand for the human reading it" counts as better than "clever demonstration of all your knowledge packed into one complex statement"

      – Oscar
      Sep 30 at 12:15











    Ok, maybe it is better to have it separately. But what is difference between 'command' and $(command) and when it is better use one method and when second method. Thanks.

    – genderbee
    Sep 30 at 11:50




    Ok, maybe it is better to have it separately. But what is difference between 'command' and $(command) and when it is better use one method and when second method. Thanks.

    – genderbee
    Sep 30 at 11:50








    @genderbee, there is no difference, except you can't nest these backticks. However you ou could easily do echo $(echo $(echo $(echo Bazinga!))) if you enjoy spawning three subshells just for fun.

    – Oscar
    Sep 30 at 12:12




    @genderbee, there is no difference, except you can't nest these backticks. However you ou could easily do echo $(echo $(echo $(echo Bazinga!))) if you enjoy spawning three subshells just for fun.

    – Oscar
    Sep 30 at 12:12








    Bazinga......! :)

    – genderbee
    Sep 30 at 12:14



    Bazinga......! :)

    – genderbee
    Sep 30 at 12:14







    @genderbee, as for the second part of your question: it depends on how you define "better". Often "easier to understand for the human reading it" counts as better than "clever demonstration of all your knowledge packed into one complex statement"

    – Oscar
    Sep 30 at 12:15




    @genderbee, as for the second part of your question: it depends on how you define "better". Often "easier to understand for the human reading it" counts as better than "clever demonstration of all your knowledge packed into one complex statement"

    – Oscar
    Sep 30 at 12:15








    0



















    special-purpose code block (here doc).



    #!/bin/bash

    cat > itf-check-article.html <<PRE
    <tr style="background-color: rgb(250,240,240);">
    <td style="padding: 10px; text-align: center;"><pre>$(awk 'printf "%s<br/>",$0' $(ls -td ../local/log/* | head -n 1))</pre></td>
    </tr>
    PRE


    output:



    $ cat itf-check-article.html
    <tr style="background-color: rgb(250,240,240);">
    <td style="padding: 10px; text-align: center;"><pre>Start of processing orders. 0 order(s) in this run.<br/>No orders in this run<br/></pre></td>
    </tr>


    Using += operator to append to variable.



    #!/bin/bash

    declare html

    html+='<tr style="background-color: rgb(250,240,240);">n'
    html+='<td style="padding: 10px; text-align: center;"><pre>'
    html+=$(awk 'printf "%s<br/>",$0' $(ls -td ../local/log/* | head -n 1))
    html+='</pre></td>n'
    html+='</tr>'

    echo -e "$html" > itf-check-article.html





    share|improve this answer





























      0



















      special-purpose code block (here doc).



      #!/bin/bash

      cat > itf-check-article.html <<PRE
      <tr style="background-color: rgb(250,240,240);">
      <td style="padding: 10px; text-align: center;"><pre>$(awk 'printf "%s<br/>",$0' $(ls -td ../local/log/* | head -n 1))</pre></td>
      </tr>
      PRE


      output:



      $ cat itf-check-article.html
      <tr style="background-color: rgb(250,240,240);">
      <td style="padding: 10px; text-align: center;"><pre>Start of processing orders. 0 order(s) in this run.<br/>No orders in this run<br/></pre></td>
      </tr>


      Using += operator to append to variable.



      #!/bin/bash

      declare html

      html+='<tr style="background-color: rgb(250,240,240);">n'
      html+='<td style="padding: 10px; text-align: center;"><pre>'
      html+=$(awk 'printf "%s<br/>",$0' $(ls -td ../local/log/* | head -n 1))
      html+='</pre></td>n'
      html+='</tr>'

      echo -e "$html" > itf-check-article.html





      share|improve this answer



























        0













        0









        0





        special-purpose code block (here doc).



        #!/bin/bash

        cat > itf-check-article.html <<PRE
        <tr style="background-color: rgb(250,240,240);">
        <td style="padding: 10px; text-align: center;"><pre>$(awk 'printf "%s<br/>",$0' $(ls -td ../local/log/* | head -n 1))</pre></td>
        </tr>
        PRE


        output:



        $ cat itf-check-article.html
        <tr style="background-color: rgb(250,240,240);">
        <td style="padding: 10px; text-align: center;"><pre>Start of processing orders. 0 order(s) in this run.<br/>No orders in this run<br/></pre></td>
        </tr>


        Using += operator to append to variable.



        #!/bin/bash

        declare html

        html+='<tr style="background-color: rgb(250,240,240);">n'
        html+='<td style="padding: 10px; text-align: center;"><pre>'
        html+=$(awk 'printf "%s<br/>",$0' $(ls -td ../local/log/* | head -n 1))
        html+='</pre></td>n'
        html+='</tr>'

        echo -e "$html" > itf-check-article.html





        share|improve this answer














        special-purpose code block (here doc).



        #!/bin/bash

        cat > itf-check-article.html <<PRE
        <tr style="background-color: rgb(250,240,240);">
        <td style="padding: 10px; text-align: center;"><pre>$(awk 'printf "%s<br/>",$0' $(ls -td ../local/log/* | head -n 1))</pre></td>
        </tr>
        PRE


        output:



        $ cat itf-check-article.html
        <tr style="background-color: rgb(250,240,240);">
        <td style="padding: 10px; text-align: center;"><pre>Start of processing orders. 0 order(s) in this run.<br/>No orders in this run<br/></pre></td>
        </tr>


        Using += operator to append to variable.



        #!/bin/bash

        declare html

        html+='<tr style="background-color: rgb(250,240,240);">n'
        html+='<td style="padding: 10px; text-align: center;"><pre>'
        html+=$(awk 'printf "%s<br/>",$0' $(ls -td ../local/log/* | head -n 1))
        html+='</pre></td>n'
        html+='</tr>'

        echo -e "$html" > itf-check-article.html




        share|improve this answer













        share|improve this answer


        share|improve this answer


        edited Sep 30 at 13:27













        answered Sep 30 at 9:38



        bac0nbac0n

        1,1531 silver badge13 bronze badges


        1,1531 silver badge13 bronze badges





















            draft saved

            draft discarded

































            Thanks for contributing an answer to Ask Ubuntu!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.


            draft saved
            draft discarded






            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1177616%2fbash-command-to-variable-cat-output-is-on-1-line-pre-is-not-working%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown































            Required, but never shown












            Required, but never shown










            Required, but never shown





            Required, but never shown























            Required, but never shown












            Required, but never shown










            Required, but never shown





            Required, but never shown




            Popular posts from this blog

            Tamil (spriik) Luke uk diar | Nawigatjuun

            Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

            Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?