+ Reply to Thread
Results 1 to 3 of 3

Thread: Unix shell variables

  1. #1
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Unix shell variables

    Unix relies heavily on strings. When a variable is declared it is usually assumed to be a string. There is separate notation for declaring numbers and arrays. If a string is operated on as a number, it will be automatically converted to a number so that the operation may be done. This is because Shell uses weak typing. Unlike other languages, shell variables declared in a script remain after the script completes. To prevent this, type:

    unset var;

    at the end of the script.


    Notation for variables:

    Unix variables are set using the syntax:
    var=value;

    They are accessed with a $ sign:
    echo $var;

    Variables can also be written using curly bracket notation:
    ${variable}
    This is commonly used for string operators.


    Special variables:
    $1, $2, $3, etc. - the first, second, third, etc. inputs to the script or function
    $0 - the first word typed when calling the script. On my terminal this is always bash.
    $# - the number of inputs to the script
    $* - a list of all inputs to the script or function
    $@ - a list of all inputs to the script or function, individually quoted
    $? - the exit status or return value of the last command or function

    Environment variables:
    These variables are built into the shell:
    $LINES - the number of lines in the terminal window
    $COLUMNS - the width of the terminal window
    $TERM - the name of the terminal
    $SHELL - the name of the shell
    $PATH - a list of all paths to the current application
    $RANDOM - a random number

    String operators:
    ${string:x:y} - substring of string starting at index x and continuing for y characters.
    ${#string} - the length of string
    ${string/pattern/new} - replace the first instance of pattern in string with new
    ${string//pattern/new} - replace all instances of pattern in string with new

    Declaring an array:


    array[0]=value1;
    array[1]=value2;
    etc.

    array=([0]=value1 [1]=value2 [2]=value3);


    Most of the string operators can be applied to arrays. When they are, they operate on the elements of the array, rather than individual characters.
    Life's too short to be cool. Be a nerd.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Unix shell variables

    Great Tutorial!! +rep

  4. #3
    nikhilkhullar is offline Newbie
    Join Date
    Nov 2009
    Location
    Jannat
    Posts
    20
    Rep Power
    0

    Re: Unix shell variables

    Nice One for beginners...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. is mac really based of unix?
    By moonknight in forum Mac OS X
    Replies: 58
    Last Post: 10-26-2010, 05:56 AM
  2. What Unix OS do you use?
    By DarkLordoftheMonkeys in forum Linux/Unix General
    Replies: 39
    Last Post: 08-09-2010, 02:07 PM
  3. need help in unix c programming
    By mjumrani in forum Linux Programming and Scripting
    Replies: 1
    Last Post: 07-12-2010, 02:47 PM
  4. Unix
    By Tonyg1 in forum Linux/Unix General
    Replies: 5
    Last Post: 04-21-2010, 05:38 PM
  5. Is Linux and Unix same ?
    By Patrick in forum Computer Software/OS
    Replies: 1
    Last Post: 10-07-2007, 07:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts