\ n in the Bash line and checking for equality

I have it:

if [ "$(./bin/ft_putendl_fd_test.bin)" != "NO__\n" ]
then
    echo "Error on ft_putendl_fd."
fi

Binary outputs:, NO__\nwhere \nis actually an ASCII char 20, not just \\n.

However, I always get it Error on ft_putendl_fd..

I tried to compare with $'NO__\n', but it does not work either.

I am using Bash 3.2.

+4
source share
2 answers

The simplest solution I have found is:

if [ "$(./bin/ft_putendl_fd_test.bin | cat -e)" != "$(echo 'NO__' | cat -e)" ]
then
    echo "Error on ft_putendl_fd."
fi
+1
source

Note that the sequence "\n"will not produce a newline character. You can use $'\n'(not portable) or just insert a new line as text:

myno="NO__"$'\n'
myno="NO__
"

, , .

,

[ "$(./bin/ft_putendl_fd_test.bin)" != "NO__" ]

remove-newlines ( ). , :

x=$(./bin/ft_putendl_fd_test.bin; echo x)
x=${x%x}

[ "$x" != "NO__
" ]

:

[ "$(./bin/ft_putendl_fd_test.bin; echo x)" != "NO__
x" ]
+5

All Articles