Sina Madani
Sina Madani

Reputation: 1324

Chaining grep in bash alias

I have a bash alias for all users in /etc/profile.d/sh_aliases. One of the aliases is as follows:

alias nlanhosts = 'nmap -sn 192.168.0.* | grep "[0-9]* hosts up" | grep -o "[0-9]*"'

When I try running the above command normally (i.e. without alias or single quotes) I get a single number. However with the alias, I get 4 numbers printed out. I can't work out why the behaviour is different when aliased. I tried using it as a function and it made no difference. Are there any characters I need to escape when making aliases?

Upvotes: 0

Views: 606

Answers (2)

user6901586
user6901586

Reputation: 88

Does this work?

alias nlanhosts='nmap -sn 192.168.0.* | grep -o "[0-9]* hosts up" | grep -o "[0-9]*"'

Just added -o for the first grep also.

Also try to use 192.168.0.0/24 or 192.168.0.1-255 format than *

Upvotes: 1

Stuart
Stuart

Reputation: 6795

You should use a function really, as your chain of commands is more complex than suited for a simple alias.

You could add:

nlanhosts() {
    nmap -sn 192.168.0.* | grep "[0-9]* hosts up" | grep -o "[0-9]*"
}

to your .bashrc file in the same way you would an alias.

Upvotes: 1

Related Questions