Language Translator

Hacking Zone

Hacking Tools
Attacking

Configure Windows

Windows Configuration

Novels

Mix Novels

Human Personality

Body Language
Variable Assignment in Shell Scripting PDF Print E-mail
Written by Hemanshu   
Tuesday, 23 September 2008

Variable Assignment in Shell Scripting



How assignment Operator works in Shell scripting?

=

the assignment operator (no space before and after)

Caution

Do not confuse this with = and -eq, which test, rather than assign!

Note that = can be either an assignment or a test operator, depending on context.

Example: Plain Variable Assignment

#!/bin/bash

# Naked variables

echo

# When is a variable "naked", i.e., lacking the '$' in front?

# When it is being assigned, rather than referenced.

# Assignment

a=879

echo "The value of "a" is $a."

# Assignment using 'let'

let a=16+5

echo "The value of "a" is now $a."

echo

# In a 'for' loop (really, a type of disguised assignment):

echo -n "Values of "a" in the loop are: "

for a in 7 8 9 11

do

echo -n "$a "

done

echo

echo

# In a 'read' statement (also a type of assignment):

echo -n "Enter "a" "

read a

echo "The value of "a" is now $a."

echo

exit 0

Example: Variable Assignment, plain and fancy

#!/bin/bash

a=23 # Simple case

echo $a

b=$a

echo $b

# Now, getting a little bit fancier (command substitution).

a=`echo Hello!` # Assigns result of 'echo' command to 'a'

echo $a

# Note that including an exclamation mark (!) within a

#+ command substitution construct #+ will not work from the command line,

#+ since this triggers the Bash "history mechanism."

# Inside a script, however, the history functions are disabled.

a=`ls -l` # Assigns result of 'ls -l' command to 'a'

echo $a # Unquoted, however, removes tabs and newlines.

echo

echo "$a" # The quoted variable preserves whitespace.

# (See the chapter on "Quoting.")

exit 0

Variable assignment using the $(...) mechanism (a newer method than backquotes). This is actually a form of command substitution.

# From /etc/rc.d/rc.local

R=$(cat /etc/redhat-release)

arch=$(uname -m)





Digg!Reddit!Del.icio.us!Google!Live!Facebook!Slashdot!Netscape!Technorati!StumbleUpon!Spurl!Wists!Simpy!Newsvine!Blinklist!Furl!Fark!Blogmarks!Yahoo!Smarking!Netvouz!Shadows!RawSugar!Ma.gnolia!PlugIM!Squidoo!BlogMemes!FeedMeLinks!BlinkBits!Tailrank!linkaGoGo!Free social bookmarking plugins and extensions for Joomla! websites! title=
Comments
Add NewSearch
Only registered users can write comments!

Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved.

 
< Prev   Next >
Your Ad Here

Donate us!!

Enter Amount:

RSS socialnet

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits
Powered password keylogger is a driver-based software keylogger by Eltima

Tired of MS Office ? Try Ashampoo Office 2008 . All OS supported