Serious Shell Programming
  • Introduction
  • Acknowledgements
  • Basics
    • Strings
      • Single-Quotes
      • Double-Quotes
      • Unquoted Strings
      • Compound Strings
    • Here Documents
      • Here Doc
      • Indented Here Doc
      • Literal Here Doc
      • In-Memory Here Doc
    • Conditionals
      • Built-in test
      • Parameter Conditionals
      • Parameter test
    • Regex
      • grep
      • awk
      • pcre
    • Control Flow
      • Binary Operators
      • if-elif-else
      • case Statement
      • for Loop
      • while Loop
      • Functions
  • shellcheck
    • Introduction
    • Bad Advice
  • Style
    • awk
    • case
    • Redirection
    • Comments
    • trap
  • String Functions
    • substr
    • sprintf
    • replace
    • replaceall
    • replacestart
    • replaceend
    • fnmatch
  • awk
    • Pre-declaring Arrays
    • Sorting Arrays
  • Know Your limits
    • Arguments
    • Environment Variables
    • Solutions
Powered by GitBook
On this page

Was this helpful?

  1. Style

Redirection

Redirection to a file should have a single space after the operator before the file. If the file contains a variable, it should be quoted. File descriptor alterations should have no whitespace. All redirections should occur at the end of the line. Do not quote static paths unless they contain special characters (~!#$*();`'"<>?\).

exec 3<&1
echo 123 > somefile
echo 456 >> somefile
echo abc > "$file"
echo def >> "$file"
kill -1 "$pid" > /dev/null 2>&1

Here document redirections should appear at the end of the line.

cat >> file <<EOF
Added to file
EOF
if some condition; then
    cat >> file <<-EOF
    Added to file
    EOF
fi
PreviouscaseNextComments

Last updated 5 years ago

Was this helpful?