L
L
Leks2016-06-24 07:26:36
linux
Leks, 2016-06-24 07:26:36

How does bash understand which variable to operate on?

Good afternoon.
In the process of writing the script, I encountered this moment:
There is a file with text:

1212 | 4956666666 | 0000

parse this file with a simple script to get only the value up to the first "|":
#!/bin/bash
cat text | grep $1 | awk -F "|" {'print $1'}

[email protected]:~/sripts$ bash -x ./test 6666
+ cat text
+ grep 6666
+ awk -F '|' '{print $1}'
1212

and even get the desired value.
But how did bash understand that in the line cat text | grep $1 | awk -F "|" {'print $1'} :
grep $1 (here we take the first input parameter),
and here awk -F "|" {'print $1'} take the first result of splitting a string with awk?
Why didn't I interpret the string as cat text | grep 6666 | awk -F "|" {'print 6666'} ?
If without a specific example, then I can formulate the question as follows:
How are conflicts of variables avoided when executing the script ./test $1 $2 $3 $4 and programs (awk, sed, etc.) which use the same variables inside the script in their internal syntax?
Ufff I hope I wrote in detail and clearly)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2016-06-24
@Leksnsk

Because the second one is inside single quotes. String inside singles is not interpolated

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question