Get count of lines in scripts (shell)

If you have tried to get the count of lines in file, the you would know about “nl” or “wc -l”. But as you are aware these give you number of lines with other details as well and you need to post process the number to make sure that you have only number and nothing else. In such cases, it is useful to use the count feature of grep and here is a shorthand to get the count of lines in any shell script:

lines=$(grep -c . <filename>)

bash – refer the first parameter from last command quickly

To refer to first parameter from the last command quickly, you can use


Note: This can also be referred as “!!:1

bash refer parameters from the current command

In bash you can use


to refer to any parameter from the current command, example:

mv ak amit.!#:1.txt
ls amit.ak.txt