Alias grep quote usage

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'm trying to make an alias in the bashrc file but having issues. I want the command to grep the access logs of our server for a particular IP address. My current entry is:



alias ip_usage='sudo grep "$1" /srv/logs/httpd/chris-server.com/access.log'


My shell usage is:



ip_usage 192.168.0.0


but this brings back all records in the access log.



I also tried inverting the quote usage but the performance was the same.



alias ip_usage="sudo grep '$1' /srv/logs/httpd/chris-server.com/access.log"


Running:



sudo grep '192.168.0.0' /srv/logs/httpd/chris-server.com/access.log


brings my back the records I want so it is something with my quotes and the value being passed in.







share|improve this question
















  • 2




    This sounds like something better suited to place into a function than an alias.
    – DopeGhoti
    Nov 17 '17 at 21:16










  • @DopeGhoti You were correct, a function was the correct route.
    – user116042
    Nov 17 '17 at 21:28















up vote
1
down vote

favorite












I'm trying to make an alias in the bashrc file but having issues. I want the command to grep the access logs of our server for a particular IP address. My current entry is:



alias ip_usage='sudo grep "$1" /srv/logs/httpd/chris-server.com/access.log'


My shell usage is:



ip_usage 192.168.0.0


but this brings back all records in the access log.



I also tried inverting the quote usage but the performance was the same.



alias ip_usage="sudo grep '$1' /srv/logs/httpd/chris-server.com/access.log"


Running:



sudo grep '192.168.0.0' /srv/logs/httpd/chris-server.com/access.log


brings my back the records I want so it is something with my quotes and the value being passed in.







share|improve this question
















  • 2




    This sounds like something better suited to place into a function than an alias.
    – DopeGhoti
    Nov 17 '17 at 21:16










  • @DopeGhoti You were correct, a function was the correct route.
    – user116042
    Nov 17 '17 at 21:28













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm trying to make an alias in the bashrc file but having issues. I want the command to grep the access logs of our server for a particular IP address. My current entry is:



alias ip_usage='sudo grep "$1" /srv/logs/httpd/chris-server.com/access.log'


My shell usage is:



ip_usage 192.168.0.0


but this brings back all records in the access log.



I also tried inverting the quote usage but the performance was the same.



alias ip_usage="sudo grep '$1' /srv/logs/httpd/chris-server.com/access.log"


Running:



sudo grep '192.168.0.0' /srv/logs/httpd/chris-server.com/access.log


brings my back the records I want so it is something with my quotes and the value being passed in.







share|improve this question












I'm trying to make an alias in the bashrc file but having issues. I want the command to grep the access logs of our server for a particular IP address. My current entry is:



alias ip_usage='sudo grep "$1" /srv/logs/httpd/chris-server.com/access.log'


My shell usage is:



ip_usage 192.168.0.0


but this brings back all records in the access log.



I also tried inverting the quote usage but the performance was the same.



alias ip_usage="sudo grep '$1' /srv/logs/httpd/chris-server.com/access.log"


Running:



sudo grep '192.168.0.0' /srv/logs/httpd/chris-server.com/access.log


brings my back the records I want so it is something with my quotes and the value being passed in.









share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '17 at 21:06







user116042














  • 2




    This sounds like something better suited to place into a function than an alias.
    – DopeGhoti
    Nov 17 '17 at 21:16










  • @DopeGhoti You were correct, a function was the correct route.
    – user116042
    Nov 17 '17 at 21:28













  • 2




    This sounds like something better suited to place into a function than an alias.
    – DopeGhoti
    Nov 17 '17 at 21:16










  • @DopeGhoti You were correct, a function was the correct route.
    – user116042
    Nov 17 '17 at 21:28








2




2




This sounds like something better suited to place into a function than an alias.
– DopeGhoti
Nov 17 '17 at 21:16




This sounds like something better suited to place into a function than an alias.
– DopeGhoti
Nov 17 '17 at 21:16












@DopeGhoti You were correct, a function was the correct route.
– user116042
Nov 17 '17 at 21:28





@DopeGhoti You were correct, a function was the correct route.
– user116042
Nov 17 '17 at 21:28











1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










The first version of the quoting would be correct except that aliases don't do what you want. You need a function:



ip_usage() sudo grep "$1" /srv/logs/httpd/chris-server.com/access.log; 


Documentation



From man bash:




Aliases allow a string to be substituted for a word when it is used as the first word of a simple command.




In other words, aliases do string substitution. They do not support manipulation of arguments. Thus, referencing a first argument inside an alias via $1 is not supported.



Also from man bash:




A shell function, defined as described above under SHELL GRAMMAR,
stores a series of commands for later execution. When the name of a
shell function is used as a simple command name, the list of
commands associated with that function name is executed. Functions
are executed in the context of the current shell; no new process is
created to interpret them (contrast this with the execution of a
shell script). When a function is executed, the arguments to the
function become the positional parameters during its execution. The
special parameter # is updated to reflect the change. Special
parameter 0 is unchanged. The first element of the FUNCNAME variable
is set to the
name of the function while the function is executing.




In other words, bash functions do support positional arguments.



Aside: Why an alias with $1 at the end might seem to work



Let's define an alias



$ alias e='echo $1'


Now, let's clear the shell's positional argument and run the alias:



$ set -- 
$ e a b c
a b c


It does what one might hope.



Notice, though, that there is a trap. Let's set the shell's first positional argument:



$ set -- First
$ echo "$1"
First


Now, let's run our command again:



$ e a b c
First a b c


Here, it is clear that $1 in an alias refers to the first shell's first argument, not the aliases first argument.






share|improve this answer






















  • Strange, I have alias svn_diff='svn diff --diff-cmd /usr/bin/diff -x "-i -b" $1' working, where $1 is a file path. I have confirmed this works for the ip_usage. I'm not following the difference between functions and aliases. It seems like the above example uses positional arguments.
    – user116042
    Nov 17 '17 at 21:22







  • 1




    @chris85 Yes, that works but only accident because the $1 is at the end of the alias where the arguments you supply would appear (with or without the $1 at the end).
    – John1024
    Nov 17 '17 at 21:24






  • 1




    Ah, I get it. Thanks.
    – user116042
    Nov 17 '17 at 21:25






  • 1




    @chris85 Very good. Also, I just added a longer explanation to the answer of how aliases work with $1.
    – John1024
    Nov 17 '17 at 21:31










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405348%2falias-grep-quote-usage%23new-answer', 'question_page');

);

Post as a guest





























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










The first version of the quoting would be correct except that aliases don't do what you want. You need a function:



ip_usage() sudo grep "$1" /srv/logs/httpd/chris-server.com/access.log; 


Documentation



From man bash:




Aliases allow a string to be substituted for a word when it is used as the first word of a simple command.




In other words, aliases do string substitution. They do not support manipulation of arguments. Thus, referencing a first argument inside an alias via $1 is not supported.



Also from man bash:




A shell function, defined as described above under SHELL GRAMMAR,
stores a series of commands for later execution. When the name of a
shell function is used as a simple command name, the list of
commands associated with that function name is executed. Functions
are executed in the context of the current shell; no new process is
created to interpret them (contrast this with the execution of a
shell script). When a function is executed, the arguments to the
function become the positional parameters during its execution. The
special parameter # is updated to reflect the change. Special
parameter 0 is unchanged. The first element of the FUNCNAME variable
is set to the
name of the function while the function is executing.




In other words, bash functions do support positional arguments.



Aside: Why an alias with $1 at the end might seem to work



Let's define an alias



$ alias e='echo $1'


Now, let's clear the shell's positional argument and run the alias:



$ set -- 
$ e a b c
a b c


It does what one might hope.



Notice, though, that there is a trap. Let's set the shell's first positional argument:



$ set -- First
$ echo "$1"
First


Now, let's run our command again:



$ e a b c
First a b c


Here, it is clear that $1 in an alias refers to the first shell's first argument, not the aliases first argument.






share|improve this answer






















  • Strange, I have alias svn_diff='svn diff --diff-cmd /usr/bin/diff -x "-i -b" $1' working, where $1 is a file path. I have confirmed this works for the ip_usage. I'm not following the difference between functions and aliases. It seems like the above example uses positional arguments.
    – user116042
    Nov 17 '17 at 21:22







  • 1




    @chris85 Yes, that works but only accident because the $1 is at the end of the alias where the arguments you supply would appear (with or without the $1 at the end).
    – John1024
    Nov 17 '17 at 21:24






  • 1




    Ah, I get it. Thanks.
    – user116042
    Nov 17 '17 at 21:25






  • 1




    @chris85 Very good. Also, I just added a longer explanation to the answer of how aliases work with $1.
    – John1024
    Nov 17 '17 at 21:31














up vote
5
down vote



accepted










The first version of the quoting would be correct except that aliases don't do what you want. You need a function:



ip_usage() sudo grep "$1" /srv/logs/httpd/chris-server.com/access.log; 


Documentation



From man bash:




Aliases allow a string to be substituted for a word when it is used as the first word of a simple command.




In other words, aliases do string substitution. They do not support manipulation of arguments. Thus, referencing a first argument inside an alias via $1 is not supported.



Also from man bash:




A shell function, defined as described above under SHELL GRAMMAR,
stores a series of commands for later execution. When the name of a
shell function is used as a simple command name, the list of
commands associated with that function name is executed. Functions
are executed in the context of the current shell; no new process is
created to interpret them (contrast this with the execution of a
shell script). When a function is executed, the arguments to the
function become the positional parameters during its execution. The
special parameter # is updated to reflect the change. Special
parameter 0 is unchanged. The first element of the FUNCNAME variable
is set to the
name of the function while the function is executing.




In other words, bash functions do support positional arguments.



Aside: Why an alias with $1 at the end might seem to work



Let's define an alias



$ alias e='echo $1'


Now, let's clear the shell's positional argument and run the alias:



$ set -- 
$ e a b c
a b c


It does what one might hope.



Notice, though, that there is a trap. Let's set the shell's first positional argument:



$ set -- First
$ echo "$1"
First


Now, let's run our command again:



$ e a b c
First a b c


Here, it is clear that $1 in an alias refers to the first shell's first argument, not the aliases first argument.






share|improve this answer






















  • Strange, I have alias svn_diff='svn diff --diff-cmd /usr/bin/diff -x "-i -b" $1' working, where $1 is a file path. I have confirmed this works for the ip_usage. I'm not following the difference between functions and aliases. It seems like the above example uses positional arguments.
    – user116042
    Nov 17 '17 at 21:22







  • 1




    @chris85 Yes, that works but only accident because the $1 is at the end of the alias where the arguments you supply would appear (with or without the $1 at the end).
    – John1024
    Nov 17 '17 at 21:24






  • 1




    Ah, I get it. Thanks.
    – user116042
    Nov 17 '17 at 21:25






  • 1




    @chris85 Very good. Also, I just added a longer explanation to the answer of how aliases work with $1.
    – John1024
    Nov 17 '17 at 21:31












up vote
5
down vote



accepted







up vote
5
down vote



accepted






The first version of the quoting would be correct except that aliases don't do what you want. You need a function:



ip_usage() sudo grep "$1" /srv/logs/httpd/chris-server.com/access.log; 


Documentation



From man bash:




Aliases allow a string to be substituted for a word when it is used as the first word of a simple command.




In other words, aliases do string substitution. They do not support manipulation of arguments. Thus, referencing a first argument inside an alias via $1 is not supported.



Also from man bash:




A shell function, defined as described above under SHELL GRAMMAR,
stores a series of commands for later execution. When the name of a
shell function is used as a simple command name, the list of
commands associated with that function name is executed. Functions
are executed in the context of the current shell; no new process is
created to interpret them (contrast this with the execution of a
shell script). When a function is executed, the arguments to the
function become the positional parameters during its execution. The
special parameter # is updated to reflect the change. Special
parameter 0 is unchanged. The first element of the FUNCNAME variable
is set to the
name of the function while the function is executing.




In other words, bash functions do support positional arguments.



Aside: Why an alias with $1 at the end might seem to work



Let's define an alias



$ alias e='echo $1'


Now, let's clear the shell's positional argument and run the alias:



$ set -- 
$ e a b c
a b c


It does what one might hope.



Notice, though, that there is a trap. Let's set the shell's first positional argument:



$ set -- First
$ echo "$1"
First


Now, let's run our command again:



$ e a b c
First a b c


Here, it is clear that $1 in an alias refers to the first shell's first argument, not the aliases first argument.






share|improve this answer














The first version of the quoting would be correct except that aliases don't do what you want. You need a function:



ip_usage() sudo grep "$1" /srv/logs/httpd/chris-server.com/access.log; 


Documentation



From man bash:




Aliases allow a string to be substituted for a word when it is used as the first word of a simple command.




In other words, aliases do string substitution. They do not support manipulation of arguments. Thus, referencing a first argument inside an alias via $1 is not supported.



Also from man bash:




A shell function, defined as described above under SHELL GRAMMAR,
stores a series of commands for later execution. When the name of a
shell function is used as a simple command name, the list of
commands associated with that function name is executed. Functions
are executed in the context of the current shell; no new process is
created to interpret them (contrast this with the execution of a
shell script). When a function is executed, the arguments to the
function become the positional parameters during its execution. The
special parameter # is updated to reflect the change. Special
parameter 0 is unchanged. The first element of the FUNCNAME variable
is set to the
name of the function while the function is executing.




In other words, bash functions do support positional arguments.



Aside: Why an alias with $1 at the end might seem to work



Let's define an alias



$ alias e='echo $1'


Now, let's clear the shell's positional argument and run the alias:



$ set -- 
$ e a b c
a b c


It does what one might hope.



Notice, though, that there is a trap. Let's set the shell's first positional argument:



$ set -- First
$ echo "$1"
First


Now, let's run our command again:



$ e a b c
First a b c


Here, it is clear that $1 in an alias refers to the first shell's first argument, not the aliases first argument.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '17 at 21:30

























answered Nov 17 '17 at 21:17









John1024

44.2k4100117




44.2k4100117











  • Strange, I have alias svn_diff='svn diff --diff-cmd /usr/bin/diff -x "-i -b" $1' working, where $1 is a file path. I have confirmed this works for the ip_usage. I'm not following the difference between functions and aliases. It seems like the above example uses positional arguments.
    – user116042
    Nov 17 '17 at 21:22







  • 1




    @chris85 Yes, that works but only accident because the $1 is at the end of the alias where the arguments you supply would appear (with or without the $1 at the end).
    – John1024
    Nov 17 '17 at 21:24






  • 1




    Ah, I get it. Thanks.
    – user116042
    Nov 17 '17 at 21:25






  • 1




    @chris85 Very good. Also, I just added a longer explanation to the answer of how aliases work with $1.
    – John1024
    Nov 17 '17 at 21:31
















  • Strange, I have alias svn_diff='svn diff --diff-cmd /usr/bin/diff -x "-i -b" $1' working, where $1 is a file path. I have confirmed this works for the ip_usage. I'm not following the difference between functions and aliases. It seems like the above example uses positional arguments.
    – user116042
    Nov 17 '17 at 21:22







  • 1




    @chris85 Yes, that works but only accident because the $1 is at the end of the alias where the arguments you supply would appear (with or without the $1 at the end).
    – John1024
    Nov 17 '17 at 21:24






  • 1




    Ah, I get it. Thanks.
    – user116042
    Nov 17 '17 at 21:25






  • 1




    @chris85 Very good. Also, I just added a longer explanation to the answer of how aliases work with $1.
    – John1024
    Nov 17 '17 at 21:31















Strange, I have alias svn_diff='svn diff --diff-cmd /usr/bin/diff -x "-i -b" $1' working, where $1 is a file path. I have confirmed this works for the ip_usage. I'm not following the difference between functions and aliases. It seems like the above example uses positional arguments.
– user116042
Nov 17 '17 at 21:22





Strange, I have alias svn_diff='svn diff --diff-cmd /usr/bin/diff -x "-i -b" $1' working, where $1 is a file path. I have confirmed this works for the ip_usage. I'm not following the difference between functions and aliases. It seems like the above example uses positional arguments.
– user116042
Nov 17 '17 at 21:22





1




1




@chris85 Yes, that works but only accident because the $1 is at the end of the alias where the arguments you supply would appear (with or without the $1 at the end).
– John1024
Nov 17 '17 at 21:24




@chris85 Yes, that works but only accident because the $1 is at the end of the alias where the arguments you supply would appear (with or without the $1 at the end).
– John1024
Nov 17 '17 at 21:24




1




1




Ah, I get it. Thanks.
– user116042
Nov 17 '17 at 21:25




Ah, I get it. Thanks.
– user116042
Nov 17 '17 at 21:25




1




1




@chris85 Very good. Also, I just added a longer explanation to the answer of how aliases work with $1.
– John1024
Nov 17 '17 at 21:31




@chris85 Very good. Also, I just added a longer explanation to the answer of how aliases work with $1.
– John1024
Nov 17 '17 at 21:31

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405348%2falias-grep-quote-usage%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay