I have a simple code here but am having some trouble understanding it.
$ more $1 | head -$2 | tail -1
For the second parameter, I am running a test txt
$ more joe.txt
i am trying
something
else
to
see
if
head
works
the way
it
should
work
When I run the script I get the error:
$ ./sample.sh jad.txt joe.txt
head: invalid option -- 'j'
Try 'head --help' for more information.
It seems to work when I remove the -
in front of the $2
.
This is a practice question I found online and I doubt they made a mistake with this one. So either I'm missing something here am doing something wrong.
When I run it without the -
in front of $2
I get this:
./sample.sh jad.txt joe.txt
it
My questions here are:
- Why does it print "it" only when
tail -1
is used? To my understanding tail prints the last 10 lines. So what does the-1
do? - Is the
-
symbol supposed to do something in front of the$2
?
-
before a parameter usually means that it's a short (one character) option to the program.--
usually means that it's a long (multiple characters) option. This only by convention though. There are lots of exceptions.So, when running:
your line
becomes:
And that makes
head
complain. You've given it the option-joe.txt
which is interpreted as the short option-j
buthead
doesn't have a-j
option.tail -1
means print the last1
line(s) from the input.tail -2
means print the last2
line(s) from the input.etc...
head -1
means print the first1
lines(s) from the input.head -2
means print the first2
lines(s) from the input.etc...