LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-15-2011, 04:48 PM   #1
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: Fedora14,Scientific 6.1?, Mandriva 2010 ;GO MAGEIA!!!Next up Gentoo
Posts: 774

Rep: Reputation: 51
Variable Substitution


I'm trying to understand just about anyting about BASH Variable Substitution. The part I'm reading is here. ://freebooks.by.ru/view/ShellProgIn24h/31480057.htm So as I don't break forum rules and am not going to contact the publisher put a http in front of the colon.

Any info will be appreciated!

Last edited by theKbStockpiler; 04-15-2011 at 04:49 PM.
 
Old 04-15-2011, 05:02 PM   #2
Didier Spaier
Senior Member
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slackware{,64}-{14.1,current} on a Lenovo Thinkpad T61 6457-4XG
Posts: 4,037

Rep: Reputation: 974Reputation: 974Reputation: 974Reputation: 974Reputation: 974Reputation: 974Reputation: 974Reputation: 974
What is your question?
 
Old 04-15-2011, 05:15 PM   #3
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: Fedora14,Scientific 6.1?, Mandriva 2010 ;GO MAGEIA!!!Next up Gentoo
Posts: 774

Original Poster
Rep: Reputation: 51
I don't know

I don't even understand enough of it to ask a questions except What is it and Why would you use it?


Is it like over-riding a variable so if you don't over-ride it, it goes to the default value?

Edit: I think it just hit me. It substitutes the name of a variable so when you call that particular varible (parameter in the guides words) you are acutually calling a differernt variable that holds a different value?

Last edited by theKbStockpiler; 04-15-2011 at 05:21 PM.
 
Old 04-15-2011, 10:17 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
I'd recommend reading the Shell Expansions section in the Bash Reference Manual instead.

Here is a trivial example on substitution:

I needed a script that searches files in the directories I specify, or all filesystems (starting at root /) if I don't specify any directories.
Normally, if you don't give any paths to find, it starts the search at the current directory.

You could use
Code:
#!/bin/bash
if [ $# -gt 0 ]; then
    find "$@" -type f
else
    find / -type f
fi
or similar if-then-else constructs, but you do not have to, since
Code:
#!/bin/bash
find "${@:-/}" -type f
does the exact same thing using substitution.

Here, $@ is the special variable that expands to all parameters given on the command line, and $# expands to the number of parameters specified on the command line. They work just like all other shell variables, they just have funny names.

${variable:-value} expands to the value of variable, if the value is not empty. Otherwise it will expand to value.

Other substitutions I've used often are ${variable##pattern}, where pattern is a glob pattern (similar to file name patterns). The longest initial match (if any) is removed from the value of variable. I also use # for the shortest initial match, % for the shortest final match, and %% for the longest final match a lot.

Here's another real life example, this time a lot more complex:

For example, assume you have
Code:
pair="the name field = the value field"
which you wish to parse to a name and a value, converting all linear whitespace to single spaces, and trimming whitespace from both name and value. The usual answer is to use sed:
Code:
name="`echo "$pair" | sed -e 's|[\t\n\v\f\r ]\+| |g; s|^ *\([^=]*[^= ]\) *=.*$|\1|g'`"
value="`echo "$pair" | sed -e 's|[\t\n\v\f\r ]\+| |g; s|^[^=]*= *||'`"
but you can also do it directly in Bash, using variable substitutions:
Code:
LWS="`echo -ne '\t\n\v\f\r '`"

name="${pair//[$LWS]/ }"	# Convert whitespaces to spaces
name="${name//    / }"		# Combine whitespaces
name="${name//   / }"		# Combine whitespaces
name="${name//  / }"		# Combine whitespaces
value="${name#*=}"		# Copy pair with name removed to value
name="${name%%=*}"		# Remove value part from name
value="${value# }"		# Remove leading space from value
value="${value% }"		# Remove trailing space from value
name="${name# }"		# Remove leading space from name
name="${name% }"		# Remove trailing space from value
I run a number of tests on my x86-64 machine (doing the above conversions in a Bash loop). The sed method takes about 7.5ms per pair, but the substitution method takes only about 0.6ms per pair. If you work with say a large configuration file, using substitutions instead of sed cuts the running time to one tenth!

Personally I also like how the parameter substitutions are easy to comment. (It is possible to comment sed patterns, too, but I find it messy.)
 
1 members found this post helpful.
Old 04-15-2011, 10:41 PM   #5
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: Fedora14,Scientific 6.1?, Mandriva 2010 ;GO MAGEIA!!!Next up Gentoo
Posts: 774

Original Poster
Rep: Reputation: 51
Ah.. Thanks so much Nominal Animal for the indepth explanation!

First time I have printed out a LQ reply on paper so it would be easier to go over. Your efforts are worth a tree!

Last edited by theKbStockpiler; 04-15-2011 at 11:05 PM.
 
Old 04-15-2011, 11:01 PM   #6
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: Fedora14,Scientific 6.1?, Mandriva 2010 ;GO MAGEIA!!!Next up Gentoo
Posts: 774

Original Poster
Rep: Reputation: 51
A few additional questions.

Quote:
${variable:-value} expands to the value of variable, if the value is not empty. Otherwise it will expand to value.
How can "value" expand unless it is a variable? It looks like it is an expression in which "Value" is both a Variable name and a Varable value.

Thanks in advance!
 
Old 04-16-2011, 12:17 AM   #7
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by theKbStockpiler View Post
How can "value" expand unless it is a variable?
It does not. value is just a value, not a variable.

Let me reiterate:

expression ${foo:-bar} will expand to the value of variable foo, if variable foo is set and not empty. Otherwise, the expression will expand to bar.

Note that bar is not a variable, it is a string value. If you want the expression to expand to the value of variable var if variable foo is unset or empty, then you use expression ${foo:-$var}.

You can chain the expressions, too. ${foo:-${bar:-${baz:-Nothing at all}}} is completely valid, and will expand to the value of variable foo. If foo is unset or empty, the expression will expand to the value of variable bar. If foo and bar are both unset or empty, the expression will expand to the value of variable baz. If variable baz is also unset or empty, the expression will expand to the string Nothing at all.
 
1 members found this post helpful.
Old 04-16-2011, 02:21 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,439

Rep: Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879
Another reference as well to help: http://tldp.org/LDP/abs/html/paramet...stitution.html
I found the examples fairly straight forward and easy to follow.
 
1 members found this post helpful.
  


Reply

Tags
bash, substitution, 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
sed substitution of variable with a variable ngyz86 Linux - Newbie 6 01-05-2011 07:44 AM
SH: Recursive Variable Substitution TVT Programming 7 07-01-2010 03:30 PM
[SOLVED] bash variable substitution Jerry Mcguire Programming 6 04-29-2010 09:33 AM
variable substitution in sed gaynut Programming 1 07-14-2008 07:38 AM
Bash variable substitution daYz Programming 3 04-14-2006 01:16 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration