Skip to main content
added 739 characters in body
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

Let's look at your first example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print $2}'

This sets the awk variable to the second shell script parameterparameter¹. The awk code then ignores that variable and prints the second field of each line in its stdin, ie. xyz for the one and only line. Note that were you to print ttl you'd find it empty.

Basically, the first $2 is a shell variableparameter expansion, evaluatedperformed by the shell, and the second is an awk field (the $ operator applied to the number 2, you could also have written it $ 2 or $ (1 + 1)...), evaluated for each line of stdin by awk.

Now let's consider the second example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print ttl}'

This sets the awk variable to the second shell script parameterparameter¹. The awk code then prints that variable, which is empty, for each line of stdin.

In the shell context, $2 is the second parameter to a shell script. When you run this directly from the command line there is no "second parameter"parameter"² so it's empty. However, when you run it as a script it may have a value.

#!/bin/sh -
echoprintf "This'This is \$2$2: $2"%s\n' "$2"

Save that in the file demo, make it executable chmod a+x demo. Now run it and observe:

./demo one
./demo one two
./demo one two three

Unfortunately you haven't told us what you're trying to achieve, so I can't suggest a solution for your requirement. But hopefully you'll get there with the information in these answers.

If you wanted for ttl to specify which field you want to output, you'd use:

$ echo "abc xyz" | awk -v ttl=2 '{print $ ttl}'
xyz

¹ though beware that with the -v var=value syntax, escape sequences such as \n, \v... in the value are expanded by awk, so if $2 contains \n, ttl will contain a newline character instead of \n

² unless you have run set one two three beforehand or possibly 2=two or argv=(one two three) in some shells which are ways to change the positional parameters dynamically in a shell.

Let's look at your first example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print $2}'

This sets the awk variable to the second shell script parameter. The awk code then ignores that variable and prints the second field of its stdin, ie. xyz. Note that were you to print ttl you'd find it empty.

Basically, the first $2 is a shell variable, evaluated by the shell, and the second is an awk field, evaluated for each line of stdin by awk.

Now let's consider the second example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print ttl}'

This sets the awk variable to the second shell script parameter. The awk code then prints that variable, which is empty, for each line of stdin.

In the shell context, $2 is the second parameter to a shell script. When you run this directly from the command line there is no "second parameter" so it's empty. However, when you run it as a script it may have a value.

#!/bin/sh
echo "This is \$2: $2"

Save that in the file demo, make it executable chmod a+x demo. Now run it and observe:

./demo one
./demo one two
./demo one two three

Unfortunately you haven't told us what you're trying to achieve, so I can't suggest a solution for your requirement. But hopefully you'll get there with the information in these answers.

Let's look at your first example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print $2}'

This sets the awk variable to the second shell script parameter¹. The awk code then ignores that variable and prints the second field of each line in its stdin, ie. xyz for the one and only line. Note that were you to print ttl you'd find it empty.

Basically, the first $2 is a shell parameter expansion, performed by the shell, and the second is an awk field (the $ operator applied to the number 2, you could also have written it $ 2 or $ (1 + 1)...), evaluated for each line of stdin by awk.

Now let's consider the second example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print ttl}'

This sets the awk variable to the second shell script parameter¹. The awk code then prints that variable, which is empty, for each line of stdin.

In the shell context, $2 is the second parameter to a shell script. When you run this directly from the command line there is no "second parameter"² so it's empty. However, when you run it as a script it may have a value.

#!/bin/sh -
printf 'This is $2: %s\n' "$2"

Save that in the file demo, make it executable chmod a+x demo. Now run it and observe:

./demo one
./demo one two
./demo one two three

Unfortunately you haven't told us what you're trying to achieve, so I can't suggest a solution for your requirement. But hopefully you'll get there with the information in these answers.

If you wanted for ttl to specify which field you want to output, you'd use:

$ echo "abc xyz" | awk -v ttl=2 '{print $ ttl}'
xyz

¹ though beware that with the -v var=value syntax, escape sequences such as \n, \v... in the value are expanded by awk, so if $2 contains \n, ttl will contain a newline character instead of \n

² unless you have run set one two three beforehand or possibly 2=two or argv=(one two three) in some shells which are ways to change the positional parameters dynamically in a shell.

Tweak
Source Link
Chris Davies
  • 128.1k
  • 16
  • 179
  • 324

YourLet's look at your first example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print $2}'

This sets the awk variable to the second shell script parameter. The awk code then ignores that variable and prints the second field of its stdin, ie. xyz. Note that were you to print ttl you'd find it empty.

Basically, the first $2 is a shell variable, evaluated by the shell, and the second is an awk field, evaluated for each line of stdin by awk.

Now let's consider the second example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print ttl}'

This sets the awk variable to the second shell script parameter. The awk code then prints that variable, which is empty, for each line of stdin.

In the shell context, $2 is the second parameter to a shell script. When you run this directly from the command line there is no "second parameter" so it's empty. However, when you run it as a script it may have a value.

#!/bin/sh
echo "This is \$2: $2"

Save that in the file demo, make it executable chmod a+x demo. Now run it and observe:

./demo one
./demo one two
./demo one two three

Unfortunately you haven't told us what you're trying to achieve, so I can't suggest a solution for your requirement. But hopefully you'll get there with the information in these answers.

Your first example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print $2}'

This sets the awk variable to the second shell script parameter. The awk code then ignores that variable and prints the second field of its stdin, ie. xyz. Note that were you to print ttl you'd find it empty.

Basically, the first $2 is a shell variable, evaluated by the shell, and the second is an awk field, evaluated for each line of stdin by awk.

Now let's consider the second example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print ttl}'

This sets the awk variable to the second shell script parameter. The awk code then prints that variable, which is empty, for each line of stdin.

In the shell context, $2 is the second parameter to a shell script. When you run this directly from the command line there is no "second parameter" so it's empty

#!/bin/sh
echo "This is \$2: $2"

Save that in the file demo, make it executable chmod a+x demo. Now run it and observe:

./demo one
./demo one two
./demo one two three

Unfortunately you haven't told us what you're trying to achieve, so I can't suggest a solution for your requirement. But hopefully you'll get there with the information in these answers.

Let's look at your first example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print $2}'

This sets the awk variable to the second shell script parameter. The awk code then ignores that variable and prints the second field of its stdin, ie. xyz. Note that were you to print ttl you'd find it empty.

Basically, the first $2 is a shell variable, evaluated by the shell, and the second is an awk field, evaluated for each line of stdin by awk.

Now let's consider the second example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print ttl}'

This sets the awk variable to the second shell script parameter. The awk code then prints that variable, which is empty, for each line of stdin.

In the shell context, $2 is the second parameter to a shell script. When you run this directly from the command line there is no "second parameter" so it's empty. However, when you run it as a script it may have a value.

#!/bin/sh
echo "This is \$2: $2"

Save that in the file demo, make it executable chmod a+x demo. Now run it and observe:

./demo one
./demo one two
./demo one two three

Unfortunately you haven't told us what you're trying to achieve, so I can't suggest a solution for your requirement. But hopefully you'll get there with the information in these answers.

additional explanation
Source Link
Chris Davies
  • 128.1k
  • 16
  • 179
  • 324

You're misunderstanding what's going on.

Your first example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print $2}'

This sets the awk variable to the second shell script parameter. The awk code then ignores that variable and prints the second field of its stdin, ie. xyz. Note that were you to print ttl you'd find it empty.

NoteBasically, the first $2 is a shell variable, evaluated by the shell, and the second is an awk field, evaluated for each line of stdin by awk.

Now let's consider the second example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print ttl}'

This sets the awk variable to the second shell script parameter. The awk code then prints that variable, which is empty, for each line of stdin.

In the shell context, $2 is the second parameter to a shell script. When you run this directly from the command line there is no "second parameter" so it's empty

#!/bin/sh
echo "This is \$2: $2"

Save that in the file demo, make it executable chmod a+x demo. NoteNow run it and observe:

./demo one
./demo one two
./demo one two three

Unfortunately you haven't told us what you're trying to achieve, so I can't suggest a solution for your requirement. But hopefully you'll get there with the information in these answers.

You're misunderstanding what's going on.

Your first example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print $2}'

This sets the awk variable to the second shell script parameter. The awk code then ignores that variable and prints the second field of its stdin, ie. xyz. Note that were you to print ttl you'd find it empty.

Note let's consider the second example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print ttl}'

This sets the awk variable to the second shell script parameter. The awk code then prints that variable, which is empty, for each line of stdin.

In the shell context, $2 is the second parameter to a shell script. When you run this directly from the command line there is no "second parameter" so it's empty

#!/bin/sh
echo "This is \$2: $2"

Save that in the file demo, make it executable chmod a+x demo. Note run it and observe:

./demo one
./demo one two
./demo one two three

Your first example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print $2}'

This sets the awk variable to the second shell script parameter. The awk code then ignores that variable and prints the second field of its stdin, ie. xyz. Note that were you to print ttl you'd find it empty.

Basically, the first $2 is a shell variable, evaluated by the shell, and the second is an awk field, evaluated for each line of stdin by awk.

Now let's consider the second example:

echo "abc xyz" | /usr/bin/awk -v ttl="$2" '{print ttl}'

This sets the awk variable to the second shell script parameter. The awk code then prints that variable, which is empty, for each line of stdin.

In the shell context, $2 is the second parameter to a shell script. When you run this directly from the command line there is no "second parameter" so it's empty

#!/bin/sh
echo "This is \$2: $2"

Save that in the file demo, make it executable chmod a+x demo. Now run it and observe:

./demo one
./demo one two
./demo one two three

Unfortunately you haven't told us what you're trying to achieve, so I can't suggest a solution for your requirement. But hopefully you'll get there with the information in these answers.

Source Link
Chris Davies
  • 128.1k
  • 16
  • 179
  • 324
Loading