LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-28-2007, 02:57 PM   #1
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
${stuff} in shell


What does putting ${ } around statements in shell do? Sometimes I see it done with legitimate variables, and other times with expressions. What's the deal? Thanks
 
Old 06-28-2007, 03:05 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
it makes quoting variables easier, for example you can't do "echo thisisthenumber$itest" where $i is a variable, but "echo thisisstillthe${i}test" will substitute just fine. it's also used when you're doing regular expression substitutions directly in bash, i'd give you an example but i can never find them online, as mos people just use grep or awk instead. so basially evaluates the expression in the curly brackets in terms of bash variables and other bash functions.
 
Old 06-28-2007, 03:13 PM   #3
aikempshall
Member
 
Registered: Nov 2003
Location: Bristol, Britain
Distribution: Slackware
Posts: 900

Rep: Reputation: 153Reputation: 153
It's used when embedded amongst other text like -

%echo ${TERM}like
vt100like

%echo $TERM
vt100

%echo ${TERM}
vt100

I find it good practice to always enclose variables with braces

try

%echo $TERMlike

and see what you get.

Alex
 
Old 06-28-2007, 03:29 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
From info bash:
Code:
       (list) list is executed in a subshell environment  (see  COMMAND  EXECUTION  ENVIRONMENT
              below).   Variable assignments and builtin commands that affect the shell’s envi-
              ronment do not remain in effect after the command completes.  The  return  status
              is the exit status of list.

       { list; }
              list  is  simply  executed in the current shell environment.  list must be termi-
              nated with a newline or semicolon.  This is known as a group command.  The return
              status  is the exit status of list.  Note that unlike the metacharacters ( and ),
              { and } are reserved words and must occur where a reserved word is  permitted  to
              be recognized.  Since they do not cause a word break, they must be separated from
              list by whitespace.
 
Old 06-28-2007, 03:34 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Here are some links to bash's parameter expansion, which makes use of the ${...} construct (I believe this is what acid_kewpie was referring to):

Bash Reference manual: 3.5.3 Shell Parameter Expansion
The Deep, Dark Secrets of Bash
bash String Manipulations

Using bash internals instead of external programs (sed,awk, grep etc), is faster and resource friendlier.

Hope this helps.
 
Old 06-28-2007, 03:50 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Also in regular expression, { } are used to repeat a previous expression. e.g.:

cat somefile|egrep '[0-9]{1,3}\.'

Looks for 1 to 3 occurences of a numeral, followed by a "." (part of the expression used to find IP addresses)
 
  


Reply

Tags
expression, sh, variable



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Shell tip: Set the shell prompt and themes in Linux Terminal LXer Syndicated Linux News 0 06-12-2007 03:02 AM
'sh' shell - Actually calls legacy Bourne shell, or uses system default? Dtsazza Linux - Software 1 10-28-2005 09:20 AM
Shell Scripting (For my wireless card stuff) chiefreborn Linux - General 9 08-31-2005 06:32 PM
When installing new stuff in suse 9.1, do you uninstall old stuff first? randon SUSE / openSUSE 1 12-25-2004 04:37 PM
Basic shell stuff drsanchez Linux - General 10 05-28-2003 12:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration