POSIX catch newline in case statement
The dash
shell does not have C-strings ($'...'
). C-strings is an extension to the POSIX standard. You would have to use a literal newline. This is easier (and looks nicer) if you store the newline in a variable:
#!/bin/dash
nl='
'
for string; do
case $string in
*"$nl"*)
printf '"%s" contains newline\n' "$string"
;;
*)
printf '"%s" does not contain newline\n' "$string"
esac
done
For each command line argument given to the script, this detects whether it contains a newline or not. The variable used in the case
statement ($string
) does not need quoting, and the ;;
after the last case
label is not needed.
Testing (from an interactive zsh
shell, which is where the dquote>
secondary prompt comes from):
$ dash script.sh "hello world" "hello
dquote> world"
"hello world" does not contain newline
"hello
world" contains newline
You can include a literal newline (in quotes) as the pattern, just as you did in assigning to the variable:
q='
'
case "$q" in
*'
'*) echo nl;;
*) echo NO nl;;
esac
This makes the formatting ugly (you cannot indent the end quote), but should be fully portable. I tested in bash, zsh, ksh, and dash.