title: Logging inside a bash script file
body: <p>I have a pretty large script ( functions contains around 4000 lines of code) . Here is a part of it :  </p>
  
  <pre><code>#!/bin/bash 
  
  
  . ./functions &gt; /dev/null 2&gt;&amp;1
  
  some_function(){
  
  while true
  do
  
  CHOICE=$(whiptail --menu "\n\n\n\n\n\n\n\n" --title "Tools" --nocancel $window 20 \
  "1" "Option1" \
  "2" "Option2" \
  "3" "Option3" 3&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;3)
  
  
  case $CHOICE in
  
      1)echo $1 $2 $3;;
      2)echo $2 $1 $3;;                                       
      3)echo $3 $2 $1;;
  
  esac
  done
  }
  
  
  
  while true; do 
  arr=()
  for ((n=1; n&lt;=$node_num; n++))
          do
          node+=($n NODE$n)
  done
  
  
  OPTION=$(whiptail --menu "\n\n\n\nPlease choose:\n\n\n" --title "tools" $window 20 "${node[@]}" \
  
  case $OPTION in
  
          1) some_function 1 2 3 ;;  
          2) some_function 2 1 3 ;;
          3) some_function 3 1 2 ;;
  esac
  done
  </code></pre>
  
  <p>I want to log the commands executed in the script.  </p>
  
  <p>What I have tried so far is : </p>
  
  <ol>
  <li><code>#!/bin/bash -x</code>   --> this will log all the output , but will also "spam" the logs with unneeded information like variable values etc. However this seems to be the best way so far... </li>
  <li>I have tried  <code>#!/bin/bash -i</code> , enabling history with set -o history . The disadvantage of this is it will log everything . When I call the function file for example it will log every single line as if it was executed . </li>
  <li><p>I have tried creating a log function : </p>
  
  <pre><code>logthis(){
      ## print the command to the logfile
      echo "$(date) $@" &gt;&gt; $historyfile
      ## run the command and redirect it's error output
      ## to the logfile
      eval "$@" 2&gt;&gt; $historyfile
  }
  </code></pre>
  
  <p>This seems to work most of the time. But when I do, for example: </p>
  
  <pre><code>case $OPTION in
      1) logthis "some_function 1 2 3" ;;  
      2) some_function 2 1 3 ;;
      3) some_function 3 1 2 ;;
  esac
  </code></pre></li>
  </ol>
  
  <p>it will not work as I will lose the arguments 1 2 3 </p>
  
  <p>Do you have any other ideas of doing an elegant logging system inside a bash script? </p>