Welcome to the Linux Foundation Forum!

Syntax for comparing numbers "-op" (operator)

Hello,

My question is regarding the following lesson:

Course/Chapter 15: The Bash Shell and Basic Scripting/Constructs/Numerical tests

On the bottom of the page, it is mentioned that :

"The syntax for comparing numbers is as follows:

exp1 -op exp2"

I have tried to find out more about the -op operator, but I could not find anything.

I would appreciate if anyone could shed some light on this operator. Thanks.

Answers

  • "-op" is a generic place holder for the real operators. The possible operators are "eq" (equal), "ne" (not equal), "lt" (less than), "le" (less than or equal), "gt" (greater than), and "ge" (great than or equal). All of these require a leading hyphen (-), for example, -eq. So, as in your example, "exp1 -eq exp2".

    For more details, consult the manual page: man bash. This is a long manual page!!! I suggest you type "/-eq" (without the quotes) to find the first occurrence of -eq in the manual page. Move one like up (up-arrow) and you will see the "arg1 OP arg2" details. Learning how to use and practice using the "man" command to look stuff up will really help you in your journey through Linux. I just used it to give you the above information! ;-) Good luck on your continued journey.

  • josepmaria
    josepmaria Posts: 79

    @KevinCSmallwood thanks for your promt and accurate answer. I appreciate it.

    If you do not mind, I would be grateful if you could provide me some additional guidance to find info about /-eq on the man pages.

    So far, I have typed the following commands, but I can't find the accurate info I would like to find;

    $ man bash /-eq
    $ man bash -k /-eq
    $ man bash -a /-eq

    These commands take me to a generic man bash page, where I have not yet found the info regarding the operators.

    Thank you very much for your time and patience.

  • Let me clarify. First type the man command for whatever manual page you seek. By the way, it doesn't hurt to look at the "man" manual page, as in: "man man" to see what options and things you can do within the "man" command. Oh, another word about reading manual (man) pages: It takes practice. They are boring. Some are better than others. Learn to skim them. Notice that they have different sections. If you are looking for a certain option (e.g., -l) to use with a command, you will see a summary at the top of the manual page. Usually, a paragraph or so later, you will see details about each of the options that are available for that particular command. Some man pages are very long! You can read it from top to bottom, but, again, they are detailed and boring. Experienced Linux users generally just skim and look for things that they are looking for (again, something like, "Which option do I use to show me keywords?" etc.). Some man pages are short. Some man pages include great examples and provide a bit of a tutorial; many don't. The more you use the "man" system, the more you get used to finding things you are looking for. I usually use man pages as a reference. I know a little bit about the command and vaguely remember that there is an option or arguments to do what I want to do. I just need the man page to remind me of the details.

    Anyway, in this specific case, you were looking up details about the numeric comparison operators in the bash shell. So, first thing you do is:

    $ man bash
    you now see the top of the very long bash manual page
    within the "man" command (while looking at the manual page), type "/-eq" followed by the enter key
    you will now skip ahead in the manual page for the first occurrence of the string "-eq"
    in this case, if you then type the up-arrow key, you will see a header in the manual page for bash that shows you the
    "arg1 op arg2" line that starts this section that you are interested in--you don't have to do the up-arrow, but I was trying
    to get you to the start of the section that you were interested in; the up-arrow key just moves you up one line
    to quit out of the "man" command, type a 'q'
    $

    There are things you can do while inside the "man" command. That includes searching for a string. The '/' operator (again while inside running the "man" command) will search for the first occurrence of the string you put after the '/' (such as "/-eq"). By the way, type an 'n' after doing that search will take you to the Next occurrence of the string (again, in this case "-eq"). This allows you to search for a particular string that is of interest to you in a man page. Again, look at "man man" for a lot more information about the "man" command!

    Another tip: Unless you have photographic memory, just plan on using the "man" command a few times a day. There is so much in Linux, you will NOT remember it all. You don't have to! Just know remember a few things and know how to use the "man" command. It's all there! All these Linux commands you are learning in this course; look them up using the "man" command and find out that there are lots of extra options to those commands that we don't talk about. Some I've never used in my 40+ years of using UNIX/Linux! LOL

    So, you want to know another use of the "man bash" manual page along with the newly learned search ('/') command? Use "/alias" inside the "man bash" command to find out more about aliases that we previously talked about (NOTE, you may need to use the 'n' key to search forward more times since the string "alias" may occur several times in the man page and not be the detail about alias that you seek). Want to know more about the "ls" command? "man ls"--more than you will ever want to know about the "ls" command! LOL Way back in 1980, I had a mentor who told me to read a manual page a day (or a week, if you prefer). Back in 1980, the number of commands was about 1/5 of the number today! How do you eat a very large quantity of food? One bite at a time.

    I hope this helps everyone who reads it.

  • josepmaria
    josepmaria Posts: 79

    @KevinCSmallwood thank you for your prompt and detailed explanation. Now I understand better how to use man pages. Now it is time to practice! Once again, thanks for your time and help.

Categories

Upcoming Training