Open Neved4 opened 10 months ago
For validation, we could use something like grok.sh or https://stackoverflow.com/a/67261832.
grok.sh
Run code as nsh -c '<code>', e.g.: nsh -c 'var='.
nsh -c '<code>'
nsh -c 'var='
[ ] Empty variable assignment
#!/bin/sh var=
nsh: parse error: --> 2:5 | 2 | var=␊ | ^--- | = expected literal_span, tilde_span, any_string_span, any_char_span, command_span, proc_subst_direction, backtick_span, expr_span, param_ex_span, param_span, double_quoted_span, single_quoted_span, or initializer
␊
[ ] Default value substitution with whitespace
echo "${var:- }"
nsh: parse error: --> 1:14 | 1 | echo "${var:- }" | ^--- | = expected word
[ ] Default value initialization
: "${var:=def}"
nsh: command not found `:'
[ ] Get name without extension
echo ${FILENAME%.*}
nsh: parse error: --> 1:16 | 1 | echo ${FILENAME%.*} | ^--- | = expected param_op
[ ] Get extension
echo ${FILENAME##*.}
nsh: parse error: --> 1:16 | 1 | echo ${FILENAME##*.} | ^--- | = expected param_op
[ ] Get directory name
echo ${PATHNAME%/*}
nsh: parse error: --> 1:16 | 1 | echo ${PATHNAME%/*} | ^--- | = expected param_op
[ ] Get file name
echo ${PATHNAME##*/}
nsh: parse error: --> 1:16 | 1 | echo ${PATHNAME##*/} | ^--- | = expected param_op
var
${var-default}
$var
""
default
${var=default}
${var?default}
exit 127
${var+default}
${var:-default}
${var:=default}
${var:?default}
${var:+default}
For POSIX parameter expansion, see: POSIX.1-2017: Parameter Expansion. For other applications of parameter expansion, see: The Bash Hackers Wiki: Parameter Expansion. For a more historical reference, see: The Traditional Bourne Shell Family.
For validation, we could use something like
grok.sh
or https://stackoverflow.com/a/67261832.Run code as
nsh -c '<code>'
, e.g.:nsh -c 'var='
.Features
[ ] Empty variable assignment
Notice how
␊
is shown when there's a newline.[ ] Default value substitution with whitespace
[ ] Default value initialization
[ ] Get name without extension
[ ] Get extension
[ ] Get directory name
[ ] Get file name
var
setvar
emptyvar
unset${var-default}
$var
""
default
${var=default}
$var
""
default
${var?default}
$var
""
exit 127
${var+default}
default
default
""
${var:-default}
$var
default
default
${var:=default}
$var
default
default
${var:?default}
$var
exit 127
exit 127
${var:+default}
default
""
""
For POSIX parameter expansion, see: POSIX.1-2017: Parameter Expansion.
For other applications of parameter expansion, see: The Bash Hackers Wiki: Parameter Expansion.
For a more historical reference, see: The Traditional Bourne Shell Family.