0

I use the following command to count lines of code (skipping blank lines and comments) in python scripts:

sed '/^\s*#/d;/^\s*$/d' *.py | wc -l

This works just fine, and I am trying to turn this into an alias.

Seems that I am not escaping some characters properly, because this:

alias loc="sed '/^\s*#/d;/^\s*$/d' \$1 | wc -l"

does not work.

Kulfy
  • 17,416
  • 26
  • 64
  • 103
  • You problem isn't to do with characters needing to be escaped, aliases just don't support positional parameters like `$1`. See for example [Can I pass arguments to an alias command?](https://askubuntu.com/questions/626458/can-i-pass-arguments-to-an-alias-command) – steeldriver Feb 05 '21 at 11:44
  • @steeldriver Aha, I see! Thank you! I changed it to: `loc() { sed '/^\s*#/d;/^\s*$/d' $1 | wc -l }` but calling this with: loc *.py does not produce the same output I expected, what am I doing wrong here? When I call, in the terminal: `sed '/^\s*#/d;/^\s*$/d' *.py | wc -l` it produces a different result. – Dominic Feb 05 '21 at 11:53
  • When you run `loc *.py` the shell expands `*.py` and passes the result to `loc`, where you only use `$1` (i.e. the first matching file). Try changing `$1` to `"$@"`. – steeldriver Feb 05 '21 at 12:01
  • @steeldriver You were right, $@ did the trick, thank you so much! – Dominic Feb 05 '21 at 13:20

1 Answers1

0

With the help of steeldriver in the comments, I've created a function instead

loc() {   sed '/^\s*#/d;/^\s*$/d' $@ | wc -l }

which solved my problem

Kulfy
  • 17,416
  • 26
  • 64
  • 103