title: sed is not working for commenting a line in a file using bash script
body: <p>I have created a bash script that is used to modify the ulimit of open files in the RHEL server.</p>
  
  <p>so i have reading the lines in the file /etc/security/limits.conf and if the soft/hard limit of the open files are less than 10000 for '*' domain i am commenting the line and adding a new line with soft/hard limit as 10000.</p>
  
  <p>The Script is working as designed but the sed command to comment a line in the script is not working.</p>
  
  <p>Please find the full script below :-</p>
  
  <pre><code>#!/bin/sh
  #This script would be called by '' to set ulimit values for open files in unix servers.
  #
  configfile=/etc/security/limits.conf
  help(){
  echo "usage: $0 &lt;LimitValue&gt;"
  echo -e "where\t--LimitValue= No of files you want all the users to open"
  exit 1
  }
  modifyulimit()
  {
  grep '*\s*hard\s*nofile\s*' $configfile | while read -r line ; do
          firstChar="$(echo $line |  xargs | cut -c1-1)"
          if [ "$firstChar" != "#" ];then
          hardValue="$(echo  $line | rev | cut -d ' ' -f1 | rev)"
              if [[ "$hardValue" -ge "$1" ]]; then
              echo ""
              else
              sed -i -e 's/$line/#$line/g' $configfile 
              echo "*          hard     nofile         $1" &gt;&gt; $configfile
              fi
          else
              echo ""
          fi
      done
  
  grep '*\s*soft\s*nofile\s*' $configfile | while read -r line ; do
          firstChar="$(echo $line |  xargs | cut -c1-1)"
          if [ "$firstChar" != "#" ];then
          hardValue="$(echo  $line | rev | cut -d ' ' -f1 | rev)"
              if [[ "$hardValue" -ge "$1" ]]; then
              echo ""
              else
              sed -i -e 's/$line/#$line/g' $configfile 
              echo "*          hard     nofile         $1" &gt;&gt; $configfile
              fi
          else
              echo ""
          fi
      done
  }
  deleteEofTag(){
  sed -i "/\b\(End of file\)\b/d" $configfile
  }
  addEofTag()
  {
  echo "#################End of file###################" &gt;&gt; $configfile
  }
  #-------------Execution of the script starts here ----------------------
  if [ $# -ne 1 ];
  then
  help
  else
  modifyulimit $1
  deleteEofTag
  addEofTag
  fi
  </code></pre>
  
  <p>The command sed -i -e 's/$line/#$line/g' $configfile when executed from the terminal is working absolutely fine and it is commenting the line but it is not working when i am executing it from the unix shell script.</p>