Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

Rep: Reputation: 209Reputation: 209Reputation: 209
${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
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971
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
Registered: Nov 2003
Location: Bristol, Britain
Distribution: Slackware
Posts: 426

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

%echo ${TERM}like

%echo $TERM

%echo ${TERM}

I find it good practice to always enclose variables with braces


%echo $TERMlike

and see what you get.

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

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
From info bash:
       (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
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381

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
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 735Reputation: 735Reputation: 735Reputation: 735Reputation: 735Reputation: 735Reputation: 735
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)


expression, sh, variable

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

All times are GMT -5. The time now is 11:32 AM.

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