difference between $variable and ${variable}


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting difference between $variable and ${variable}
# 1  
Old 04-12-2007
difference between $variable and ${variable}

Hi there

Simple question im sure but what is the difference between $variable and ${variable}

ie I have seen scripts like this

Code:
for user in gary peter paul ; do
   chown -R ${user}:other /data/trans
done



and some that do this

Code:
for user in gary peter paul ; do
   chown -R $user:other /data/trans
done


what is the difference

Cheers
# 2  
Old 04-12-2007
this might put some light on the matter:
Code:
#!/bin/bash
t=100
echo $t
echo $tea
echo ${t}ea

# 3  
Old 04-12-2007
From man sh
Code:
   Parameter Expansion
       The  ?$? character introduces parameter expansion, command substitution,
       or arithmetic expansion.  The parameter name or symbol  to  be  expanded
       may  be  enclosed in braces, which are optional but serve to protect the
       variable to be expanded from characters immediately following  it  which
       could be interpreted as part of the name.

       When  braces  are  used,  the matching ending brace is the first ?}? not
       escaped by a backslash or within a quoted  string,  and  not  within  an
       embedded  arithmetic expansion, command substitution, or paramter expan-
       sion.

       ${parameter}
              The value of parameter is substituted.  The braces  are  required
              when  parameter  is  a  positional  parameter  with more than one
              digit, or when parameter is followed by a character which is  not
              to be interpreted as part of its name.

See this

Code:
[/tmp]$ cat try.sh
#! /bin/sh

a=12
b=34
echo $a_$b
echo ${a}_$b
[vivarkey@/tmp]$ sh try.sh
34
12_34
[/tmp]$

# 4  
Old 04-12-2007
ah thankyou
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

5. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

6. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

7. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

8. Shell Programming and Scripting

Difference between use vars and our variable in PERL

What is the difference between defining the global variable through our and using use vars ? Is the variable created using our goes beyond even package scope? Thanks in Advance !!! (3 Replies)
Discussion started by: jatanig
3 Replies

9. Shell Programming and Scripting

Korn Shell Variable values difference

I am using two shell scripts a.ksh and b.ksh a.ksh 1. Sets the value +++++++++++++++++ export USER1=abcd1 export PASSWORD=xyz +++++++++++++++++ b.ksh 2. Second scripts calls sctipt a.ksh and uses the values set in a.ksh and pass to an executable demo... (2 Replies)
Discussion started by: kunalseth
2 Replies

10. UNIX for Dummies Questions & Answers

What is the difference between a shell variable that is exported and the one that is

What is the difference between a shell variable that is exported and the one that is not exported? (1 Reply)
Discussion started by: JosephGerard
1 Replies
Login or Register to Ask a Question