Variabel di Shell Script

In the previous issue we’ve become acquainted with a shell script and managed to create a very simple shell script. If we look, we did not perform any processing on the shell script. We only show a message on the screen and execute commands on the shell through shell script. What if we want to make the program more interactive shell script?

As with any programming language, shell script also serves to recognize the variables that can hold information temporarily for a variety of purposes, for example to compute or determine the output results. You may make as much as possible or use a variable in your shell script. Name the variables are independent, large and small letters should, but make it easier to remember, make it a habit to create standard rules in the manufacture of the variable name. At this writing, all variables will be written in lowercase.

Variables can be divided into two types, namely environment variables and user variables. Environment variables are variables that have been previously determined as a part of the shell used (bash). By default, the name of this variable using all capital letters. Example of this is the $ USER variable that will contain the user name you are currently using, $ HOME home directory which contains the address of the user that is used, and so forth. To display the entire value of an existing environment variable, you can use the set command in the terminal (Figure 1). User variable is the variable name specified by the user themselves and not by the shell used.

Variables can be accessed by using the dollar sign ($) before the variable name, for example, we have a variable named “my name”, to access the value stored in the variable, we use the $ my name. To give value to a variable, we use the sign “=” is immediately followed by the value we give without any spaces, for example my name = Willy. What if the value that we want to give is a sentence? Use double quotes as the opening and closing value of a variable, such as my name = “Willy Sudiarto Raharjo”. For example, see listing 1 and try running on your computer. Seen that the value of the variable Sudiarto be regarded as a command and not part of the variable because it is not enclosed in double quotation marks. Please be careful in giving a value to a variable.

You can combine environment variables and user variables in a shell script is the same, as in listing 2. What if want to write a message using the $ character, such as “It cost me $ 15”? If we are not careful, it could be a shell script would be wrong to interpret the information that we provide and try to take the value of variable 1 (which will not contain any) and display it as a “price 5”. To fix this, use the escape character to indicate that the next character will be recognized as a regular character and not as a substitute for a variable, which marks the backslash “\” as in listing 3.

One character that needs more attention is the backtick character “` “(position number 1 on the left side your keyboard) because this character has a special function in shell programming, which is able to accommodate the output of a shell command in a variable. As an example, we will hold the result of the date command into a variable date and display its contents using the echo command as in listing 4.

To be able to receive input from the user and store it into a variable, we can use the read function is followed by the name of the variable that we want to use to store the values ​​as in Example 5 listings.

About