title: Elegant way to replace tr '\n' '\0' (Null byte generating warnings at runtime)
body: <p>I strongly doubt about the <code>grep</code> best use in my code and would like to find a better and cleaner coding style for extracting the session ID and security level from my  cookie file :</p>
  
  <pre><code>cat mycookie 
  # Netscape HTTP Cookie File
  # https://curl.haxx.se/docs/http-cookies.html
  # This file was generated by libcurl! Edit at your own risk.
  
  #HttpOnly_127.0.0.1 FALSE   /   FALSE   0   PHPSESSID   1hjs18icittvqvpa4tm2lv9b12
  #HttpOnly_127.0.0.1 FALSE   /mydir/ FALSE   0   security    medium
  </code></pre>
  
  <p>The expected output is the SSID hash :</p>
  
  <pre><code>1hjs18icittvqvpa4tm2lv9b12
  </code></pre>
  
  <p>Piping <code>grep</code> with  <code>tr '\n' '\0'</code> works like a charm in the command line, but generates warnings (<code>warning: command substitution: ignored null byte in input</code>”) at the bash code execution. Here is the related code (with warnings):    </p>
  
  <pre><code>ssid=$(grep -Po 'PHPSESSID.*' path/sessionFile | grep -Po '[a-z]|[0-9]' | tr '\n' '\0')
  </code></pre>
  
  <p>I am using <code>bash 4.4.12 (x86_64-pc-linux-gnu)</code> and could read <a href="https://stackoverflow.com/a/42493691/461212">here</a> this crystal clear explanation :</p>
  
  <blockquote>
    <p>Bash variables are stored as C strings. C strings are NUL-terminated.
    They thus cannot store NULs by definition.</p>
  </blockquote>
  
  <p>I could see <a href="https://stackoverflow.com/a/32722481/461212">here</a> and <a href="https://askubuntu.com/a/926695/906724">there</a> in both cases a coding solution using <code>read</code>:</p>
  
  <pre><code># read content from stdin into array variable and a scalar variable "suffix"
  array=( )
  while IFS= read -r -d '' line; do
    array+=( "$line" )
  done &lt; &lt;(process that generates NUL stream here)
  suffix=$line # content after last NUL, if any
  
  # emit recorded content
  printf '%s\0' "${array[@]}"; printf '%s' "$suffix"
  </code></pre>
  
  <p>I don't want to use arrays nor a <code>while</code> loop for this specific case, or others. I found this workaround using <code>sed</code>:</p>
  
  <pre><code>ssid=$(grep -Po 'PHPSESSID.*' path/sessionFile | grep -Po '[a-z]|[0-9]' | tr '\n' '_' | sed -e 's/_//g')
  </code></pre>
  
  <p>My two questions are :</p>
  
  <p>1) Would it be a better way to substitute <code>tr '\n' '\0'</code>, without using <code>read</code> into a <code>while</code> loop ?<br>
  2) Would it be a better way to extract properly the SSID and security level ? </p>
  
  <p>Thx</p>