LinuxQuestions.org
Visit Jeremy's Blog.
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-06-2017, 04:10 AM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 294
Blog Entries: 2

Rep: Reputation: 16
Smile Any examples of file expansion AND work splitting?


Language: Bash/Unix Shell-scripting

Can anyone raise an example of

file expansion
word splitting?


I just can't find any examples on the net. What exactly is a file expansion??


BACKGROUND:
One of my previous posts turned into two problems, that makes it impossible to look up the second problem for reference in the future.

Last edited by andrew.comly; 06-06-2017 at 04:22 AM. Reason: clarity
 
Old 06-06-2017, 04:16 AM   #2
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 294
Blog Entries: 2

Original Poster
Rep: Reputation: 16
Smile [Solved]

Here are some examples, which originally were answered here.


Quote:
Originally Posted by rknichols View Post
You have an example of file expansion (pathname expansion) above when you typed "echo ${foo}" and saw that "[a-z]*" was expanded to "Desktop Documents Downloads ... Pictures Public".

For an example of word splitting,
Code:
Dirs="Desktop Documents Downloads"
ls -ld $Dirs
The expansion of $Dirs is word-split into separate arguments to ls. Compare that to what happens for
Code:
ls -ld "$Dirs"
where the quotes prevent the word splitting, and the ls command doesn't find anything with the 29-character name, "Desktop Documents Downloads".
Quote:
Originally Posted by pan64 View Post
filename expansion is for example: ls -ld D*
(try with and without " )
Quote:
Originally Posted by MadeInGermany View Post
File name expansion and word splitting occur in a for loop (after the in) and in command arguments.
Commands are for example ls, cp, echo, printf, test.
Also [ is a command, indeed it is an alias of test - in [ mode it expects an additional last argument ].
file name expansion means the given word (argument) is taken as a pattern, and matches are done with file names in the current directory. The matching file names replace the given word. It usually becomes longer and can become many words --> "expansion".

A word splitting occurs when an unquoted variable is substituted by its value and if the value contains spaces.
Example
Code:
var="two words"
printf "%s\n" $var
BTW during file name expansion, if a file name contains spaces it is not split into words.
Example
Code:
touch "oneword" "two words"
a="two*"
printf "%s\n" $var
And what happens if there were
Code:
a="two *"
? Variable substitution and word splitting happen first!

Last edited by andrew.comly; 06-06-2017 at 04:24 AM. Reason: clarity
 
  


Reply

Tags
bash, expansion, word splitting


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
Does IFS only work on variable expansion? glenjoker Linux - Newbie 7 10-15-2015 09:30 AM
LXer: Bash: 10 Examples of Parameter Expansion LXer Syndicated Linux News 0 11-17-2014 05:41 AM
Examples of work example sites texasone General 11 12-03-2010 12:05 PM
[SOLVED] What can be done to make these code examples work on web Browsers? Larry James Programming 22 03-12-2010 08:04 AM
Why won't that darn bootsplash thing work!? (splitting hairs) Dark Carnival Slackware 3 04-22-2005 02:26 AM

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

All times are GMT -5. The time now is 06:48 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration