Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Your "echo" command gets a string from the var.
But it's default behavior is to suppress the interpretation of sequences like "\n".
So it does, what it is designed to: it concatenates all given strings (disregarding the contained newlines), inserts a space between the strings and outputs it.
This takes place with your "echo $x".
If you quote "$var" echo gets one big string containing newline characters and as it has nothing to concatenate it outputs the string, as it is and the contained "\n" are interpreted in a normal fashion.
Some remarks.
Never parse the output of "ls" "ps" and alike.
They depend on language settings and are not portable.
In fact, if using the output of "ls" you are NOT dealing with filenames, instead you are dealing with representations of filenames.
Use find instead.
"find . -type d " gives a newline separated list of all found directories.
And to avoid the quoting hell ALWAYS quote each and every var.
Use "$var" and never, never $var.
If the use of unquoted vars seems to fit, be sure: your script logic is wrong. Definitely!
Another point is, that there are loads of "echo"s out.
It could be a binary, a bash builtin or even an alias.
If you are going to examine this deeper take care to know, which kind of "echo" you are using.
"env echo" calls it from the path.
"echo" only god i.e. the sysadmin knows.
erm. Should know.
Last edited by uhelp; 02-21-2012 at 01:15 PM.
Reason: added problem builtin vs. bin
You are observing word splitting resulting from the parameter expansion of $x, itself.
From the bash man page:
Quote:
Parameter Expansion
The `$' character introduces parameter expansion,
...
Quote:
Word Splitting
The shell scans the results of parameter expansion, command substitution, and arithmetic expansion
that did not occur within double quotes for word splitting.
...
So, word splitting usually happens to the output of a parameter expansion, except when it is in double quotes. Since newline is in $IFS, it gets stripped.
Last edited by jhwilliams; 02-21-2012 at 01:28 PM.
Your "echo" command gets a string from the var.
But it's default behavior is to suppress the interpretation of sequences like "\n".
So it does, what it is designed to: it concatenates all given strings (disregarding the contained newlines), inserts a space between the strings and outputs it.
This isn't quite right.
Echo respects newlines in input strings:
Code:
echo 'a
b'
a
b
The suppression of special chars (e.g. \n) has to do with when it is received literally to echo:
@uhelp You're right, I guess I did not actually answer the question. Also, od is a useful tool -- I had almost forgot about it. Glad you reintroduced me. :-)
Quote:
Originally Posted by towheedm
Why does quoting the variable generate the newline?
So the answer here is that it doesn't generate the newline. Rather, by not quoting the variable, the shell performs word splitting on the output of the parameter expansion.
# this gives you an array named "mydirs"
# addressing one item can be ¼{mydirs[$i] where i is an integer.
# index starts at zero
mydirs=( $( find <yourDir> -type d ) )
# if you want each entry concatenated with a space in between use ${mydirs[*]}
for x in "${mydirs[*]}" ; do
<commandlist> "$x" #this is safe even if directory name contain spaces or
# other unusual characters. It is ensured
#that only a directory name will be given
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.