From 1a30ea83dbfb08fb60ff6effd80379687b6150b7 Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 15 Apr 2017 10:23:43 -0700 Subject: [PATCH] Created SC2209 (markdown) --- SC2209.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 SC2209.md diff --git a/SC2209.md b/SC2209.md new file mode 100644 index 0000000..5739a52 --- /dev/null +++ b/SC2209.md @@ -0,0 +1,27 @@ +## Use var=$(command) to assign output (or quote to assign string). + +### Problematic code: + +```sh +user=whoami # Want to run whoami and assign output + +PAGER=cat git log # Want to assign the string "cat" +``` + +### Correct code: + +```sh +user=$(whoami) + +PAGER="cat" git log +``` + +### Rationale: + +Putting `var=` in front of a command will not assign its output. Use `var=$(my command here)` to execute the command and capture its output. + +If you do want to assign a literal string, use quotes to make this clear to shellcheck and humans alike. + +### Exceptions: + +None. \ No newline at end of file