From 55380b7066b6693e478ffa01c44d289a99b804e0 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Wed, 30 Dec 2020 20:11:33 -0800 Subject: [PATCH] Created SC2278 (markdown) --- SC2278.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 SC2278.md diff --git a/SC2278.md b/SC2278.md new file mode 100644 index 0000000..89e4b50 --- /dev/null +++ b/SC2278.md @@ -0,0 +1,32 @@ +## $0 can't be assigned in Ksh (but it does reflect the current function). + +### Problematic code: + +```sh +#!/bin/ksh +$0=myname +echo "Usage: $0 --help" +``` + +### Correct code: + +```sh +#!/bin/ksh +myname() { + echo "Usage: $0 --help" +} +myname +``` +### Rationale: + +You appear to be trying to assign a new value to `$0` in Ksh. + +This is not possible. However, `$0` will reflect the current function name, so if you wrap your code in a function with your chosen name, you can have `$0` expand to it. + +### Exceptions: + +If you instead wanted to compare the value of `$0`, use a comparison like `[ "$0" = "myname" ]`. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file