Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • Double-quote your variables when you use them so that their contents isn't parsed and word-split by the shell. For example echo $message should become echo "$message". (The curly braces are mostly unneeded.) Note that if $message begins with a dash (hyphen) all bets are off when you try to use echo anyway Commented May 7, 2021 at 6:53
  • 2
    Shouldn't message=$(echo ${tmppath}) be message=$(cat "$tmppath") to get the contents of the temporary file rather then its name Commented May 7, 2021 at 6:56
  • (1) I’m giving you a +1 for posting the best answer, complete with mktemp andrm, even though Stéphane Chazelas posted the bare bones of that answer four years earlier. Please get into the habit of reading all the existing answers before you post a new one. It’s OK to post a new answer improving on a previous post (IMO, you did that), but you should cite any such previous posts. … (Cont’d) Commented Apr 24, 2022 at 6:51
  • (Cont’d) … (2) @roaima is right: you should double-quote all your variables (e.g., complex_command > "$tmppath" and rm -f "$tmppath"), and you don’t need any of those curly braces.  (3) You could improve this answer by testing whether mktemp succeeded before you use "$tmppath". Commented Apr 24, 2022 at 6:51