0

I hope someone has an idea about my issue. The current minimal example fails, if I'm trying to assign a command/pipeline output to a variable due to the grep "[[:space:]]" expression for bash in the strict mode.

#/bin/bash
set -euo pipefail
egrep -v '>' file | grep "[[:space:]]" | wc -l
var=`egrep -v '>' file | grep "[[:space:]]" | wc -l`
echo $var

var=$(egrep -v '>' file | grep "[[:space:]]" | wc -l) fails as well. I'm out of ideas.

  • How does it fail, exactly? What is the content of file? What are you expecting to happen, and what happens instead? Note that there appears to be a typo in your shebang line (should be #! not just #). – steeldriver Aug 25 '21 at 15:16
  • @steeldriver Thank you, I corrected the shebang but did not change anything. It fails because the echo statement will never be applied since the variable assignment failed. So the egrep execution works and returns a number or zero while the variable will not setted. The exit code is 1. The script should check for white spaces inside a FASTA file sequence. – user16751033 Aug 25 '21 at 22:04
  • The script works, if "file" contains white spaces inside a sequence, but fails if there is not white space inside the sequence. Obviously, bash fails to continue the pipe if "nothing" is passed. – user16751033 Aug 25 '21 at 22:13
  • You could try var=$( { grep '[[:space:]]' file | wc -l || : ;} ) or simpler var=$( { grep -c '[[:space:]]' file_no_space || : ;} ) - see for example Prevent grep from exiting in case of nomatch – steeldriver Aug 25 '21 at 22:28
  • Thanks, i'll try that. White spaces are allowed inside a sequence header, but not inside the sequence ;-) headers are marked by > as the first character in a line. – user16751033 Aug 25 '21 at 22:38

0 Answers0