Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
$0 is the string used to invoke the script, for instance "/home/bin/mytest" or "./test2".
$# is the number of command line arguments, so the quoted example is seeing if there is exactly one command argument and setting $LocalHost to it. The 1 is therefore just a number.
$1 would be the first argument passed to the script, so for example: ./scriptname some words $0 will be ./scriptname, $1 is some and $2 is words. And $# is 2 in this case.
but there is an error if [[ $# -eq 1 ]]; then would be correct syntax
but there is an error if [[ $# -eq 1 ]]; then would be correct syntax
So, you're saying that comparing the string "1" with the string "1" for equality is insufficient and you need to make the shell convert each string to an integer and compare the two integers, right?
Unless your numbers can potentially have different representations ("1", "01", "0x1", "0x0001", etc.), a string compare for equality is fine, and that is what is commonly done. For greater-than or less-than, not so good, though. The string "10" is less than "9".
$1 would be the first argument passed to the script, so for example: ./scriptname some words $0 will be ./scriptname, $1 is some and $2 is words. And $# is 2 in this case.
but there is an error if [[ $# -eq 1 ]]; then would be correct syntax
No, either [[ $# == 1 ]] or [ $# -eq 1 ] or (( $# == 1 )) would be correct.
Last edited by cfajohnson; 01-16-2015 at 01:20 AM.
So, you're saying that comparing the string "1" with the string "1" for equality is insufficient and you need to make the shell convert each string to an integer and compare the two integers, right?
No.
I tried to say: if [[ $# = 1 ]] ; then
is not ok (as in the original post only one = was used). From the other hand using numerical comparison looks better for numbers, but you are right, actually it is completely irrelevant (compare them as number and/or strings is the same right now)
No.
I tried to say: if [[ $# = 1 ]] ; then
is not ok (as in the original post only one = was used).
You can indeed use one single "=" as well. In fact, "=" and "==" are synonymous, at least in bash. But "=" is recommended for POSIX conformance.
Code:
string comparison
=
is equal to
if [ "$a" = "$b" ]
==
is equal to
if [ "$a" == "$b" ]
This is a synonym for =
(ABS Guide: Comparison operators)
Code:
string1 == string2
string1 = string2
True if the strings are equal. = should be used with the test
command for POSIX conformance.
(man bash: Conditional Expressions)
Last edited by Philip Lacroix; 01-16-2015 at 02:49 AM.
Reason: fixed link
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.