Shell scripts will typically start with ``#!/bin/sh`` or ``#!/bin/bash``. This line, `affectionally known by various names including hashbang and shebang <http://en.wikipedia.org/wiki/Shebang_(Unix)>`_, is treated as a directive to run the rest of the script with the given interpreter.
This directive, combined with setting the execute bit via ``chmod`` tells the system that the file is meant to be executed, rather than simply a file containing text.
The same line may be used instead to specify another shell on the system, such as ``ksh``, ``tcsh``, ``zsh``, or another interpreter entirely, such as Perl, Python, or Ruby.
specifications. The `bash manual <http://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html#Bash-POSIX-Mode>`_ contains a list of these functions.
To echo out the value of this variable, use ``$ops_var`` like so:
..code-block:: console
$ echo $ops_var
1
The same variable can be assigned different values in the same script.
Creating a script called ``ops_script.sh``
..code-block:: bash
#!/usr/bin/env bash
# First an integer value
ops_var=1
echo $ops_var
# Then a string
ops_var="Hello"
echo $ops_var
When this script is executed:
..code-block:: console
$ ./ops_script.sh
1
Hello
NOTE: From this point on, assume that the same ``ops_script.sh`` is going to be used.
I also won't be typing ``#!/usr/bin/env bash`` every time, but know that it's present at the top of the script.
Variables can be used in other strings by calling them with curly braces ``{ }`` around the variable name.
..code-block:: bash
ops_var="Yoda"
echo "${ops_var}, my name is"
..code-block:: console
$ ./ops_script.sh
Yoda, my name is
You can use variables to store user input and use it later on in the script.
For example, if you want to ask the user for their name and echo it back to the screen:
..code-block:: bash
print "Hello, what is your name?: "
read name
echo "Hello, ${name}"
I'm going to supply the name "Yoda" in this case.
..code-block:: console
$ ./ops_script.sh
Hello, what is your name?: Yoda
Hello, Yoda
By default, all variable values are treated as strings, unless the operation they are used for explicitly uses them as another data type.
Assume you have two variables that you assign integer values to, and you want to add them together.
..code-block:: bash
first_var=1
second_var=2
result=${first_var}+${second_var}
echo ${result}
You would expect the output of this to be 3, but this is not the case with bash.
..code-block:: console
$ ./ops_script.sh
1+2
What happened here was that both values were treated as string and the expression ``${first_var}+${second_var}`` got evaluated as the string "1+2".
To actually add these two numbers, the operation needed is a little different.
..code-block:: bash
first_var=1
second_var=2
result=$(( ${first_var} + ${second_var}))
echo ${result}
Here, the ``$(( ))`` tells bash that anything that goes inside these double parentheses needs to be evaluated as an arithmentic operation.
..code-block:: console
$ ./ops_script.sh
3
* Built-in variables
The second kind of variables that you can use in your scripts are the ones that the shell automatically populates for you when it is started up.
These variables store information specific to your shell instance, so if you and a co-worker are both logged on to the same server, both of you might not see the same values for all built-in variables.
Here are some examples of built-in variables:
..code-block:: bash
# Home directory of the logged-on user.
$HOME
# Name of the computer you are currently logged on to
$HOSTNAME
# Present working directory
$PWD
The above variables contain some information relative to the shell itself.
But there are other built-in variables you can use within your script which store information like exit statuses of scripts/commands and number of arguments passed to a script.
..code-block:: bash
# Exit status of the previously executed script/command
$?
# Number of arguments passed to the script
$#
# Value of the 1st argument passed to the script
${1}
These variables let your script take in parameters which can be then used throughout the script.
For example, I will write a script that prints the number of parameters received, and use the first one in a string
..code-block:: bash
# Print the number of arguments passed
echo "Number of arguments: $#"
# Use the first argument in a string
echo "First argument passed was: ${1}"
I'll run this script a couple of times with different arguments to show how this works