Skip to content
Mark Vander Stel edited this page Dec 6, 2020 · 16 revisions

WIP

IFS

Always set a local IFS when doing something that references it. Never assume that the user or Liquidprompt has a sane IFS set.

If running a for loop on the output of a command, like

for temp in $(sensors -u); do
  ...

you should set a local beforehand like local IFS=$'\n'.

If using read to read data from a file or command, set IFS as part of the read command:

IFS= read var <myfile
IFS=" " read var1 var2 var3 <<<"1 2 3"

Quoting

When in doubt, always quote a shell variable ("$var").

Special shell variables like $? and $- are treated no differently by the shell, they must also be quoted.

There are two cases where quoting is optional:

  1. When setting a variable: var=$other_var. When setting a variable to something other than one variable, use quotes for clarity: var="${var1}${var2}".
  2. When referencing a variable in a [[ ... ]] block.

Both of these exceptions are not exceptions if the string you are building contains whitespace:

var=$myvar $othervar    # does not work
var="$myvar $othervar"  # fixed

[[ $var $othervar == one two ]]      # does not work
[[ "$var $othervar" == "one two" ]]  # fixed
Clone this wiki locally