Skip to main content
added 592 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

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.

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.

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.

Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

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.