Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Converted from echo to printf
Source Link
Gordon Davisson
  • 11.6k
  • 4
  • 30
  • 33

You can pipe the script's output through a loop that prefixes the current date and time:

./script.sh | while IFS= read -r line; do echoprintf '%s %s\n' "$(date)" $line";"$line"; done >>/var/log/logfile 

If you'll be using this a lot, it's easy to make a bash function to handle the loop:

adddate() { while IFS= read -r line; do printf "%s'%s %s\n"%s\n' "$(date)" "$line"; done } ./thisscript.sh | adddate >>/var/log/logfile ./thatscript.sh | adddate >>/var/log/logfile ./theotherscript.sh | adddate >>/var/log/logfile 

You can pipe the script's output through a loop that prefixes the current date and time:

./script.sh | while IFS= read -r line; do echo "$(date) $line"; done >>/var/log/logfile 

If you'll be using this a lot, it's easy to make a bash function to handle the loop:

adddate() { while IFS= read -r line; do printf "%s %s\n" "$(date)" "$line"; done } ./thisscript.sh | adddate >>/var/log/logfile ./thatscript.sh | adddate >>/var/log/logfile ./theotherscript.sh | adddate >>/var/log/logfile 

You can pipe the script's output through a loop that prefixes the current date and time:

./script.sh | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >>/var/log/logfile 

If you'll be using this a lot, it's easy to make a bash function to handle the loop:

adddate() { while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done } ./thisscript.sh | adddate >>/var/log/logfile ./thatscript.sh | adddate >>/var/log/logfile ./theotherscript.sh | adddate >>/var/log/logfile 
Answerer said printf is better than echo for this scenario in the comment
Source Link

You can pipe the script's output through a loop that prefixes the current date and time:

./script.sh | while IFS= read -r line; do echo "$(date) $line"; done >>/var/log/logfile 

If you'll be using this a lot, it's easy to make a bash function to handle the loop:

adddate() { while IFS= read -r line; do echoprintf "%s %s\n" "$(date)" $line""$line"; done } ./thisscript.sh | adddate >>/var/log/logfile ./thatscript.sh | adddate >>/var/log/logfile ./theotherscript.sh | adddate >>/var/log/logfile 

You can pipe the script's output through a loop that prefixes the current date and time:

./script.sh | while IFS= read -r line; do echo "$(date) $line"; done >>/var/log/logfile 

If you'll be using this a lot, it's easy to make a bash function to handle the loop:

adddate() { while IFS= read -r line; do echo "$(date) $line" done } ./thisscript.sh | adddate >>/var/log/logfile ./thatscript.sh | adddate >>/var/log/logfile ./theotherscript.sh | adddate >>/var/log/logfile 

You can pipe the script's output through a loop that prefixes the current date and time:

./script.sh | while IFS= read -r line; do echo "$(date) $line"; done >>/var/log/logfile 

If you'll be using this a lot, it's easy to make a bash function to handle the loop:

adddate() { while IFS= read -r line; do printf "%s %s\n" "$(date)" "$line"; done } ./thisscript.sh | adddate >>/var/log/logfile ./thatscript.sh | adddate >>/var/log/logfile ./theotherscript.sh | adddate >>/var/log/logfile 
Source Link
Gordon Davisson
  • 11.6k
  • 4
  • 30
  • 33

You can pipe the script's output through a loop that prefixes the current date and time:

./script.sh | while IFS= read -r line; do echo "$(date) $line"; done >>/var/log/logfile 

If you'll be using this a lot, it's easy to make a bash function to handle the loop:

adddate() { while IFS= read -r line; do echo "$(date) $line" done } ./thisscript.sh | adddate >>/var/log/logfile ./thatscript.sh | adddate >>/var/log/logfile ./theotherscript.sh | adddate >>/var/log/logfile