Using variables in Unix shell scripts

Any Unix shell script longer than a line will most likely involve using variables. Variables are used to store temporary values to simply using them in various Unix commands of your script. The beauty of using variables is that they can be evaluated and set ones, but then reused as many times as you like without your shell interpreter having to re-evaluate them again.

Defining a variable in Unix shell

To specify a value for a variable, you need to decide on the variable name – can be any word or combination of English alphabet symbols and digits, and specify the value.

In Bourne shell (sh), Bourne Again Shell (bash) and Korn Shell (ksh), here's how you would define a new variable and assign it a value:

CONFIG_FILE="/etc/myfile"

In C-Shell (csh), it's done like this:

setenv CONFIG_FILE "/etc/myfile"

Basic variables usage

To access the value of a variable, you need to use the same variable name, but with a dollar sign in front of it, like this:

$CONFIG_FILE

Important: to set a new value to the variable, you use just the variable name like CONFIG_FILE, but to access this value later you need to use the $CONFIG_FILE form.

The most basic way to use variables is to assign them constant values at the beginning of your script. This is a good way to define locations of standard files or directories your script will work with, etc:

#!/bin/sh
#
CONFIG_FILE=/etc/myfile
MY_DIR=/etc
echo $CONFIG_FILE

Using output of Unix commands to set variables

One of the best things about shell scripting is that it's very easy to use any Unix command to generate the output and use it to set the variable.

In this example, I'm running a date command and saving its output as values for my variables:

ubuntu$ cat /tmp/1.sh
#!/bin/sh
#
STARTED=`date`
sleep 5
FINISHED=`date`
#
echo "Script start time: $STARTED"
echo "Script finish time: $FINISHED"

If I run this simple script, I see the following:

ubuntu$ /tmp/1.sh
Script start time: Wed May 7 04:56:51 CDT 2008
Script finish time: Wed May 7 04:56:56 CDT 2008
The same approach can be used for practically any scenario.

Here's an example of using uname command to extract some useful information about our system:

#!/bin/sh
#
STARTED=`date`
NODE=`uname -n`
OS=`uname -o`
CPU=`uname -p`
FINISHED=`date`
#
echo "Nodename: $NODE"
echo "OS type: $OS"
echo "Processor: $CPU"
echo "Script start time: $STARTED"
echo "Script finish time: $FINISHED"

And this is how it works:

ubuntu$ /tmp/1.sh
Nodename: ubuntu
OS type: GNU/Linux
Processor: unknown
Script start time: Wed May  7 05:05:31 CDT 2008
Script finish time: Wed May  7 05:05:31 CDT 2008

That's it for today! Let me know what next topic about Unix shell scripting you'd like to see covered, and I'll do my best to explain it in the coming posts.

Related books

If you want to learn more, here's a great book:


linux-command-line-shell-scripting-bible

Linux Command Line Shell Scripting Bible

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
  • Pingback: Unix scripts: time and date | UNIX Tutorial: Learn UNIX()

  • ESWAR

    Hi, This tutorial is very good. I am intresting to get tips and tricks for all flavors of Unix

    Thanks

  • Pingback: Environment Variables in Unix | UNIX Tutorial: Learn UNIX()

  • Karthika

    Hi!
    I found your tutorial and suggestions very helpful Thank You!

  • Gleb Reys

    Thanks for your kind words, Karthika! Glad to have helped, see you again for more!

  • Ronaldo

    Please a have a variable _VART_ and I want to append new values to this variable, keeping the old one. How can I do that using a simplke script file?
    Thanks a lot
    Ronaldo

  • Pingback: Parsing text files line by line in Unix script | UNIX Tutorial: Learn UNIX()

  • Pingback: Updating Values of Your Shell Variables in Unix | UNIX Tutorial: Learn UNIX()

  • Pingback: Unix scripts: basic arithmetic operations | UNIX Tutorial: Learn UNIX()

  • http://www.adolfo.trinca.name adolfo.trinca

    thank you

  • Gleb Reys

    You're welcome, Adolfo!

  • sree

    Thanks for the article Gleb.
    Could you please tell me the difference between the two statements

    var1=$var2
    var1={$var2}

  • Pingback: Unix scripts: how to sum numbers up — UNIX Tutorial: Learn UNIX()

  • Chandra Sekhar

    Hi. Could someone explain me how to use ! as a variable in unix if command? (It always complains me that unknown operator when I compare two variables and one variable's value is !)

    Thanks in advance.

  • Chandra Sekhar

    FYI: This is how the code is written
    if [ "$Dir" = "New" ]
    then
    echo "Correct"
    fi

    when Dir's value is ! then it throws the following error:
    new_test.sh[26]: [: New: unknown operator

  • Chandra Sekhar

    I was able to find the solution myself…. :)
    I changed the code as follows and it works!!

    if [[ "$Dir" = "New" ]]; then
    echo "Correct"
    fi

  • Chandra

    i think
    if [[ "$Dir" = "New" ]]; then
    echo "Correct"
    fi

    is much better, thanks chandra

  • suman

    Hi, i want to know the storage capacity of a shell variable i.e how many bytes they ocupy in memory?

  • kavita

    I want to name html file as tp5_7.html where s=5 and e=7. I tried using tp$s_$e.html. but it gives me only tp7.html. can you please help me here?