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 -1is used? To my understanding tail prints the last 10 lines. So what does the-1do? - 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
headcomplain. You've given it the option-joe.txtwhich is interpreted as the short option-jbutheaddoesn't have a-joption.tail -1means print the last1line(s) from the input.tail -2means print the last2line(s) from the input.etc...
head -1means print the first1lines(s) from the input.head -2means print the first2lines(s) from the input.etc...