Skip to content

Instantly share code, notes, and snippets.

@zmwangx
Created July 4, 2014 16:37
Show Gist options
  • Save zmwangx/16ac4697a6d75213fca8 to your computer and use it in GitHub Desktop.
Save zmwangx/16ac4697a6d75213fca8 to your computer and use it in GitHub Desktop.
Bash: test for newline in a string.
#!/bin/bash
# Use pattern matching: http://goo.gl/q6qNco.
# The newline in the pattern string needs to be quoted.
# See ANSI-C quoting: http://goo.gl/I3HZTR
string=$(echo -e 'hello\nworld')
if [[ ${string} == *$'\n'* ]]; then
echo 'newline detected' >&2
else
echo 'newline not detected' >&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment