0

I'm working on some script to help organize some stuff. I have simplified my code down to this from my original:

$ echo $(find -type d -maxdepth 5 | grep -E "\./([^/])*/([^/])*/([^/])*/([^/])*"
 | cut -d'/' -f 3 | sort | uniq -d | xargs -I R sh -c 'echo -e "alpha \n"')
(R actually doesn't do anything here but it's used in the original)

Particularly, I think something is wrong with my xargs

xargs -I R sh -c 'echo -e "alpha \n"'

What I would look to see happen is alpha to be printed several times, each on a newline. However, my output looks like

alpha alpha alpha...

I've been scouring around the internet trying to find how to fix this but it's no use. I've just started experimenting with bash, can someone please point out what I'm doing wrong?

  • 3
    Why are you wrapping the whole thing in `echo $(...)`? That makes you subject to [I just assigned a variable, but `echo $variable` shows something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else). – Charles Duffy Oct 19 '20 at 03:29
  • Charles you might just be a lifesaver. I might've been trying to fix the wrong thing all this time – Andrew the Programmer Oct 19 '20 at 03:31
  • 1
    ... That's not to say that your xargs use is correct, though. I'm not at a computer to test it or any proposed replacement right now, though. – Charles Duffy Oct 19 '20 at 03:31
  • 2
    `sh -c 'echo -e "alpha \n"'` is not going to work since `echo -e` is not POSIX and you are calling a POSIX `sh`. Use `sh -c 'printf "%s \n\n" "alpha"'` instead. – Léa Gris Oct 19 '20 at 03:36
  • This seems to be working, thank you. What exactly does %s do? – Andrew the Programmer Oct 19 '20 at 03:47
  • What are you trying to do with this syntax? You have not said that. You have written a set of pipes, finally going to xargs not using the actual arguments at all. Do you want to print "alpha" in a newline, as many times as the number of arguments? – thanasisp Oct 19 '20 at 05:02
  • I wanted to run some operations on R defined in xargs and print them out line by line. The manipulation part is easy and works in my code, however when I get the output (ie ```alpha```) it didn't print out line by line – Andrew the Programmer Oct 19 '20 at 05:04
  • @LeaGris : Not sure that this explains in this case the observed behaviour. For instance if I try it on `dash`, which is pretty close POSIX, the echo would print something like ` -e alpha` instead. The OP did not state which OS is being used, but on Linux (and similar systems), an alternative would be to use `/bin/echo` explicitly (or `command echo`). – user1934428 Oct 19 '20 at 09:06
  • 1
    @user1934428 This is why it is a comment and not an answer. As to why I recommend `printf` rather than `echo` especially when escaping special characters, here it is: https://stackoverflow.com/q/64079604/7939871 – Léa Gris Oct 19 '20 at 10:12

1 Answers1

1

This is a special case of I just assigned a variable, but echo $variable shows something else.


Just running a command, no echo:

$ printf '%s\n' "first line" "second line"
first line
second line

Running a command in an unquoted command substitution, as your code is currently written:

$ echo $(printf '%s\n' "first line" "second line")
first line second line

Running a command in a quoted command substitution:

$ echo "$(printf '%s\n' "first line" "second line")"
first line
second line

...so: If you don't have a reason for the outer echo $(...), just remove it; and if you do have a reason, add quotes.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441