bluesxman
I need to do a string comparison in a script I'm writing that will check to see if a variable contains only the newline character.
I'd like to do something like this, but it doesn't seem to recognise the "\n"
Code: Select all
if [ "${string}" = "\n" ] ; then .... ; fi
Code: Select all
if ! echo "${string}" | grep "." ; then .... ; fi
Is it possible to do this with solely internal commands?
Last edited by bluesxman (21 Jul 2010 14:56)
cmd | *sh | ruby | chef
----------------------------
#2 20 Jul 2011 05:29
flabdablet
Couple of ways. In bash, you can do
Code: Select all
if [ "$string" = $'\n' ]
then
echo String is a newline
fi
Code: Select all
newline='
'
if [ "$string" = "$newline" ] ...
Code: Select all
if [ "$string" = '
' ] ...