Arguments
Pass information to a script as you run it.
1#!/bin/bash
2echo "Hello, $1!"
3echo "You gave me $# argument(s)."
$ ./greet.sh Ada
Hello, Ada!
You gave me 1 argument(s).
Every line, explained
#!/bin/bash- This first line is called the shebang (from "hash bang", the # and ! it starts with). It is not a comment: it tells the computer which program should read the rest of the file. #!/bin/bash means "run this with bash". Every bash script starts with this exact line.
echo "Hello, $1!"- Words typed after the script's name are its arguments, and the script gets them as $1, $2, $3 and so on. This is how one script does a different job each time you run it. $1 is the first word typed after the script name. Look at the terminal: the script is run as ./greet.sh Ada, so $1 holds Ada.
echo "You gave me $# argument(s)."- $# is a counter: how many arguments arrived. It is the usual way a script checks it was given what it needs before it starts work.