Unexpected variable update when using bash's $(( )) operator for arithmetic

Posted by philo on Stack Overflow See other posts from Stack Overflow or by philo
Published on 2011-11-18T17:47:55Z Indexed on 2011/11/18 17:51 UTC
Read the original article Hit count: 190

Filed under:
|
|

I'm trying to trim a few lines from a file. I know exactly how many lines to remove (say, 2 from the top), but not how many total lines are in the file. So I tried this straightforward solution:

$ wc -l $FILENAME
119559 my_filename.txt
$ LINES=$(wc -l $FILENAME | awk '{print $1}')
$ tail -n $(($LINES - 2)) $FILENAME > $OUTPUT_FILE

The output is fine, but what happened to LINES??

$ wc -l $OUTPUT_FILE
119557 my_output_file.txt
$ echo $LINES
107

Hoping someone can help me understand what's going on.

© Stack Overflow or respective owner

Related posts about bash

Related posts about shell