Friday, November 15, 2019

command line - Passing arguments to a script

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.

No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...