I have a bunch of xml file in the current directory.
Problem 1.
As far as I have read eval returns the results and stores it in a variable. But I get an error with the below command
find ./ -name '*.xml' | file=$(eval awk '{print $0}') ; echo $file
EDIT ( after ommitting eval as pointed by cas ) -
Omitting eval just returns an empty string.
find ./ -name '*.xml' | file=$(awk '{print $0}') ; echo $file
Problem 2.
I am just trying to learn bash and hence I made a complicated sequence of diffing the first two files from the output of find. The complicated sequence is just to understand the concepts of bash programming.
find ./ -name '*.xml' | file=$(awk '{print $0}') ; echo $file && diff -y $(sed '2q;d' $file) $(sed '1q;d' $file)
eval
there. The command substitution ($(awk ...)
) is what is storing the awk output in$file
, not theeval
.eval
executes a string (whatever is on its command line) as sh code in a sub-shell, and is VERY easy to make disastrous mistakes with - I recommend forgetting it even exists until you know shell programming well enough to know the risks and how to avoid them. – cas Jan 24 '18 at 23:42eval
somewhere in the line. tryfile="$(find . -name '*.xml')"
. or, better yet, use an array. e.g.typeset -a files; files=( $(find . -name '*.xml') )
– cas Jan 25 '18 at 01:50$file
anyway because pipes are executed in a sub-shell, and sub-shells can NOT affect the environment (including variables) of their parent. this comes up quite frequently on this site, there's a good explanation here: https://unix.stackexchange.com/a/9994/7696 – cas Jan 25 '18 at 01:52