ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I wanted to take the output of 'ls' and pipe it into xargs to execute a command for each filename. It doesnt much matter which command I wanted to run so for example here I will just 'echo' the filenames to serve as a trivial example :
Create a test directory with a filename containing a single quote :
$mkdir amigo\'s_folder
Now run the example command for all files in the folder
$ls | xargs 'echo {}'
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
As you can see I get the error message shown above. This is because the filename containing a single ' character confuses xargs which treats it as a special character. Using the '-0' option does not appear to solve the problem.
Is there a command in linux that will take an input string and escape any special characters found within it. e.g a command called 'esc' that I could use as follows :
Using `find` and -exec seem to work. I figured this out based on http://www.linuxquestions.org/questi...e-quote-99830/ which was the second result Google gave me when I searched for "single quote with xargs". Here's an example:
Code:
sasha@reactor:~/test$ ls | xargs 'echo {}'
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
sasha@reactor:~/test$ find ./ -type d -exec echo "{}" \;
./
./amigo's_folder
sasha@reactor:~/test$
Hope this helps you and welcome to LQ!
Sasha
Last edited by GrapefruiTgirl; 11-27-2009 at 10:51 PM.
Reason: clarity
The '-print0' and '-0' options allow null terminating characters to delimit input and output, this avoiding any problems with whitespace or special characters.
Thanks for your replies but I think ive got an answer to my question :
First of all, the output of 'ls' is delimited by new line characters i.e. '\n'. Its a bit confusing because it seems that bash, by default, ignores them for ls and displays the results left to right. To prove this do the following :
$ls > ls.txt
$cat ls.txt
You will see that cat interprets the \n's as new lines
Is this valid reasoning?
So, given this we can use the delimiter option '-d' of xargs :
$ls | xargs -d\\n echo
Here note that since bash itself would escape '\n' as a literal 'n' character we need to make sure we pass the literal string '\n' to the command, therefore we must escape the backslash.
Not sure I understand your reasoning in the first half of your post. Pretty much everything is terminated line-by-line with \n characters. Bash of course displays stuff L to R (unless you're using a R to L language), and, bash isn't ignoring the characters, it's interpreting them, as newlines. So this is normal behavior.
For the second half of your post: The real answer is, "Does it work? Using the -d option?", and if so, then you've got s solution that involves less typing than the previous suggestions
xargs is something I don't use much at all; I should try to use it more, is it's pretty handy.
Therefore strange as it sounds, it must be bash that is interpreting the output?
How intiguing!
Code:
c:~$ ls | od > /tmp/trash
c:~$ ls -1 | od > /tmp/trash1
c:~$ diff /tmp/trash /tmp/trash1
[no output]
Alternatively ls may behave differently depending on the output device. Here's a couple of excerpts from the ls man page suggesting that ls does adapt to the output device
Code:
-w, --width=COLS
assume screen width instead of current value
...
With --color=auto, color codes are output only if standard output is connected to a terminal (tty)
Last edited by catkin; 11-30-2009 at 05:27 AM.
Reason: Cleaning, polishing, keeping the sub-editors happy ....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.