Ubuntu – Passing arguments to a script

bashcommand linescripts

I am taking the Linux Essentials class and was doing well until I hit the scripting chapter. I simply do not understand these concepts. Wondering if someone can break the following down into ultra simplistic terms or point me to a better reference to learn it. I'm currently using netacad's curriculum.


From the text book (with minor formatting changes):

There are some special variables in addition to the ones you set. You
can pass arguments to your script:

#!/bin/bash
echo "Hello $1"

A dollar sign followed by a number N corresponds to the Nth argument
passed to the script. If you call the example above with ./test.sh the
output will be Hello Linux. The $0 variable contains the name of the
script itself.

After a program runs, be it a binary or a script, it returns an exit
code which is an integer between 0 and 255. You can test this through
the $? variable to see if the previous command completed successfully.


I understand how to assign variables and how they work with the $ but the whole issue with $0 and $1 — I just don't get it.

Any help would be much appreciated.

Best Answer

The description from the book is wrong (or at least missing something). To get that script to print "Hello Linux", you'd run it like this:

./test.sh Linux

If you run it with just ./test.sh, then it'll only print "Hello ", because there was no first argument and $1 is not defined. On the other hand, suppose you ran it like this:

./test.sh foo bar baz

then within the script, $0 would be "./test.sh", $1 would be "foo", $2 would be "bar", and $3 would be "baz".

As for $?, consider the following script snippet:

ls nonexistentfile.txt
echo "The exit status of ls was: $?"
echo "The exit status of echo (the first one) was: $?"

When run that'll print something like:

ls: nonexistentfile.txt: No such file or directory
The exit status of ls was: 1
The exit status of echo (the first one) was: 0

The ls command can't list nonexistentfile.txt ('cause it doesn't exist), so it prints an error message to that effect, and exits with a nonzero status to indicate that something went wrong. The first echo command prints that exit status ($?), and since it does that successfully, it exits with a status of zero. When the second echo command runs, it gets $? from the first echo command, so it prints "0".

BTW, a lot of commands just use exit statuses of 0 (success) or 1 (some sort of failure), but some use different failure statuses to indicate exactly what went wrong. Here's an excerpt from the curl manual page:

EXIT CODES
       There are a bunch of different  error  codes  and  their  corresponding
       error  messages  that  may appear during bad conditions. At the time of
       this writing, the exit codes are:

       1      Unsupported protocol. This build of curl has no support for this
              protocol.

       2      Failed to initialize.

       3      URL malformed. The syntax was not correct.

       ...

       88     FTP chunk callback reported error

       89     No connection available, the session will be queued

       90     SSL public key does not matched pinned public key

       91     Invalid SSL certificate status.

       92     Stream error in HTTP/2 framing layer.

...so a script that used curl could check $? to figure out what went wrong, and respond differently depending on the problem.