What is shell variables in linux

What is shell variables in linux

Just about every programming language in existence has the concept of variables — a symbolic name for a chunk of memory to which we can assign values, read and manipulate its contents. The Bourne shell is no exception, and this section introduces that idea. This is taken further in Variables — Part II which looks into variables which are set for us by the environment.
Let’s look back at our first Hello World example. This could be done using variables (though it’s such a simple example that it doesn’t really warrant it!)
Note that there must be no spaces around the » = » sign: VAR=value works; VAR = value doesn’t work. In the first case, the shell sees the » = » symbol and treats the command as a variable assignment. In the second case, the shell assumes that VAR must be the name of a command and tries to execute it.
If you think about it, this makes sense — how else could you tell it to run the command VAR with its first argument being «=» and its second argument being «value»?
Enter the following code into var.sh:

#!/bin/sh
MY_MESSAGE
="Hello World" echo $MY_MESSAGE

This assigns the string «Hello World» to the variable MY_MESSAGE then echo es out the value of the variable.
Note that we need the quotes around the string Hello World. Whereas we could get away with echo Hello World because echo will take any number of parameters, a variable can only hold one value, so a string with spaces must be quoted so that the shell knows to treat it all as one. Otherwise, the shell will try to execute the command World after assigning MY_MESSAGE=Hello

The shell does not care about types of variables; they may store strings, integers, real numbers — anything you like.
People used to Perl may be quite happy with this; if you’ve grown up with C, Pascal, or worse yet Ada, this may seem quite strange.
In truth, these are all stored as strings, but routines which expect a number can treat them as such.
If you assign a string to a variable then try to add 1 to it, you will not get away with it:

$ x="hello"
$ expr $x + 1
expr: non-numeric argument
$

This is because the external program expr only expects numbers. But there is no syntactic difference between:

MY_MESSAGE="Hello World"
MY_SHORT_MESSAGE
=hi
MY_NUMBER
=1
MY_PI
=3.142
MY_OTHER_PI
="3.142"
MY_MIXED
=123abc

Note though that special characters must be properly escaped to avoid interpretation by the shell.
This is discussed further in Chapter 6, Escape Characters.

We can interactively set variable names using the read command; the following script asks you for your name then greets you personally:

#!/bin/sh echo What is your name? read MY_NAME echo "Hello $MY_NAME - hope you're well."

Mario Bacinsky kindly pointed out to me that I had originally missed out the double-quotes in the final line, which meant that the single-quote in the word «you’re» was unmatched, causing an error. It is this kind of thing which can drive a shell programmer crazy, so watch out for them!

This is using the shell-builtin command read which reads a line from standard input into the variable supplied.
Note that even if you give it your full name and don’t use double quotes around the echo command, it still outputs correctly. How is this done? With the MY_MESSAGE variable earlier we had to put double quotes around it to set it.
What happens, is that the read command automatically places quotes around its input, so that spaces are treated correctly. (You will need to quote the output, of course — e.g. echo «$MY_MESSAGE» ).

Scope of Variables

Variables in the Bourne shell do not have to be declared, as they do in languages like C. But if you try to read an undeclared variable, the result is the empty string. You get no warnings or errors. This can cause some subtle bugs — if you assign
MY_OBFUSCATED_VARIABLE=Hello
and then
echo $MY_OSFUCATED_VARIABLE
Then you will get nothing (as the second OBFUSCATED is mis-spelled).

There is a command called export which has a fundamental effect on the scope of variables. In order to really know what’s going on with your variables, you will need to understand something about how this is used.

Create a small shell script, myvar2.sh :

#!/bin/sh echo "MYVAR is: $MYVAR"
MYVAR
="hi there" echo "MYVAR is: $MYVAR"
$ ./myvar2.sh
MYVAR is
:
MYVAR is
: hi there

MYVAR hasn’t been set to any value, so it’s blank. Then we give it a value, and it has the expected result.
Now run:

$ MYVAR=hello
$
./myvar2.sh
MYVAR is
:
MYVAR is
: hi there

It’s still not been set! What’s going on?!
When you call myvar2.sh from your interactive shell, a new shell is spawned to run the script. This is partly because of the #!/bin/sh line at the start of the script, which we discussed earlier.
We need to export the variable for it to be inherited by another program — including a shell script. Type:

$ export MYVAR
$
./myvar2.sh
MYVAR is
: hello
MYVAR is
: hi there

Now look at line 3 of the script: this is changing the value of MYVAR . But there is no way that this will be passed back to your interactive shell. Try reading the value of MYVAR :

Once the shell script exits, its environment is destroyed. But MYVAR keeps its value of hello within your interactive shell.
In order to receive environment changes back from the script, we must source the script — this effectively runs the script within our own interactive shell, instead of spawning another shell to run it.
We can source a script via the «.» (dot) command:

$ MYVAR=hello
$
echo $MYVAR
hello
$
. ./myvar2.sh
MYVAR is
: hello
MYVAR is
: hi there
$
echo $MYVAR
hi there

The change has now made it out into our shell again! This is how your .profile or .bash_profile file works, for example.
Note that in this case, we don’t need to export MYVAR .
An easy mistake to make is to say echo MYVAR instead of echo $MYVAR — unlike most languages, the dollar ( $ ) symbol is required when getting the value of a variable, but must not be used when setting the value of the variable. An easy mistake to make when starting out in shell scripting.
One other thing worth mentioning at this point about variables, is to consider the following shell script:

#!/bin/sh echo "What is your name?" read USER_NAME echo "Hello $USER_NAME" echo "I will create you a file called $USER_NAME_file" touch $USER_NAME_file

Think about what result you would expect. For example, if you enter «steve» as your USER_NAME, should the script create steve_file ?
Actually, no. This will cause an error unless there is a variable called USER_NAME_file . The shell does not know where the variable ends and the rest starts. How can we define this?
The answer is, that we enclose the variable itself in curly brackets:

#!/bin/sh echo "What is your name?" read USER_NAME echo "Hello $USER_NAME" echo "I will create you a file called $_file" touch "$_file"

The shell now knows that we are referring to the variable USER_NAME and that we want it suffixed with » _file «. This can be the downfall of many a new shell script programmer, as the source of the problem can be difficult to track down.

Also note the quotes around «$_file» — if the user entered «Steve Parker» (note the space) then without the quotes, the arguments passed to touch would be Steve and Parker_file — that is, we’d effectively be saying touch Steve Parker_file , which is two files to be touch ed, not one. The quotes avoid this. Thanks to Chris for highlighting this.

Источник

Unix / Linux — Using Shell Variables

In this chapter, we will learn how to use Shell variables in Unix. A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data.

A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.

Variable Names

The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).

By convention, Unix shell variables will have their names in UPPERCASE.

The following examples are valid variable names −

Following are the examples of invalid variable names −

2_VAR -VARIABLE VAR1-VAR2 VAR_A!

The reason you cannot use other characters such as !, *, or is that these characters have a special meaning for the shell.

Defining Variables

Variables are defined as follows −

variable_name=variable_value

The above example defines the variable NAME and assigns the value «Zara Ali» to it. Variables of this type are called scalar variables. A scalar variable can hold only one value at a time.

Shell enables you to store any value you want in a variable. For example −

Accessing Values

To access the value stored in a variable, prefix its name with the dollar sign ($) −

For example, the following script will access the value of defined variable NAME and print it on STDOUT −

#!/bin/sh NAME="Zara Ali" echo $NAME

The above script will produce the following value −

Read-only Variables

Shell provides a way to mark variables as read-only by using the read-only command. After a variable is marked read-only, its value cannot be changed.

For example, the following script generates an error while trying to change the value of NAME −

#!/bin/sh NAME="Zara Ali" readonly NAME NAME="Qadiri"

The above script will generate the following result −

/bin/sh: NAME: This variable is read only.

Unsetting Variables

Unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks. Once you unset a variable, you cannot access the stored value in the variable.

Following is the syntax to unset a defined variable using the unset command −

The above command unsets the value of a defined variable. Here is a simple example that demonstrates how the command works −

#!/bin/sh NAME="Zara Ali" unset NAME echo $NAME

The above example does not print anything. You cannot use the unset command to unset variables that are marked readonly.

Variable Types

When a shell is running, three main types of variables are present −

  • Local Variables − A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. They are set at the command prompt.
  • Environment Variables − An environment variable is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually, a shell script defines only those environment variables that are needed by the programs that it runs.
  • Shell Variables − A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly. Some of these variables are environment variables whereas others are local variables.

Источник

Читайте также:  Postgresql linux connect to database
Оцените статью
Adblock
detector