Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: If line begins with >

I want an if/then statement in Bash and I can't seem to get it to work. I would like to say "If the line begins with > character, then do this, else do something else".

I have:

while IFS= read -r line
do
    if [[$line == ">"*]]
    then
        echo $line'first'
    else
        echo $line'second'
    fi
done

But it isn't working. I also tried to escape the ">" by saying:

if [[$line == ^\>*]]

Which didn't work either. Both ways I am getting this error:

line 27: [[>blah: command not found

Suggestions?

like image 331
Stephopolis Avatar asked Jan 18 '26 23:01

Stephopolis


1 Answers

Spaces are needed inside [[ and ]] as follows:

if [[ "$line" == ">"* ]]; then 
    echo "found"
else
    echo "not found"
fi
like image 137
anubhava Avatar answered Jan 20 '26 15:01

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!