From bf89411aaf07523516a6d6fe4e21072bbae0a9e9 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Thu, 8 Nov 2018 10:03:32 +0800 Subject: [PATCH] Created SC2240 (markdown) --- SC2240.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 SC2240.md diff --git a/SC2240.md b/SC2240.md new file mode 100644 index 0000000..9974041 --- /dev/null +++ b/SC2240.md @@ -0,0 +1,30 @@ +## The dot command does not support arguments in sh/dash. Set them as variables. + +### Problematic code: + +```sh +#!/bin/sh +. include/myscript example.com 80 +``` + +### Correct code: + +```sh +#!/bin/sh +host=example.com port=80 . include/myscript +``` +### Rationale: + +In Bash and Ksh, you can use `. myscript arg1 arg2..` to set `$1` and `$2` in the sourced script. + +This is not the case in Dash, where any additional arguments are ignored, or in POSIX sh where the behavior is unspecified. + +Instead, assign arguments to variables and rewrite the sourced script to read from them. + +### Exceptions: + +None. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file