Echo eval xargs and other siblings


 
Thread Tools Search this Thread
The Lounge What is on Your Mind? Echo eval xargs and other siblings
# 1  
Old 03-01-2019
Echo eval xargs and other siblings

It always seemed to me that these utils are siblings.
All they do are that substitute values for variables, rearrange the parameters, and confuse the input with the output. Smilie
I tried to display their main signature in table together. To show their similarities
Echo eval xargs and other siblings-echo_eval_xargspng
# 2  
Old 03-01-2019
Shell evaluation is done automatically, by the shell, before a command is run. None of these except eval do shell evaluation; eval can do so because it's actually part of the shell!

By the same ticket, ls abc* is not a feature of ls. abc* gets evaluated by the shell before ls is executed.

I'm not sure what your >stdin stdin> are about. eval does not use stdin or stdout especially, unless the command it's evaluating does. It has no limits at all. That's what makes it dangerous.

One unique feature of xargs is that it parses single and double quotes. Usually this is considered an annoyance and worked around, but this can be used to your advantage, i.e. to split strings containing quotes without resorting to eval.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-02-2019
Thanks @Corona688,
stdin> this is the shell stdin
I was hoping that it would be clear from the context that the stdin> is the entrance to the shell. I guess I need to work on the design of the presentation. Smilie
It seemed to me funny, for example, "echo" and "eval" commands. Both commands take parameters and do the same work. Only the first returns a string of them to the stdout and the second command to the stdin (shell stdin).
I'v added the feature of Xargs, above noted to my notes.

Last edited by nezabudka; 03-02-2019 at 07:58 AM..
# 4  
Old 03-02-2019
Quote:
Originally Posted by nezabudka
for example, "echo" and "eval" commands. Both commands take parameters and do the same work. Only the first returns a string of them to the stdout and the second command to the stdin (shell stdin).
Sorry, but this is based on a misconception of what eval does. eval has no output at all, not even to the input of the current shell.

When the shell reads an input line (that might be a commandline entered by the user or a line of code from a script being executed) it starts to interpret (or "decode") this line in a process of several fixed stages. One of these steps is - for example - the replacement of wildcard-expressions by a list of filenames fitting that wildcard-expression, another step is the replacement of variable names by their respective content.

The process goes like this: you enter i.e.

Code:
ls file*

and let us say there are three files which filenames start with "file". So the shell, when doing the respective stage, replaces the wildcard with the respective filelist:

Code:
ls fileA fileB fileC

Note that nothing is exeuted yet, the shell was just "cooking" the command line. Only now the result of this "cooking" is presented to the ls command. For ls there is no noticeable difference between these two forms of commands. For the same reason this is a scripting error:

Code:
if [ -r file* ] ; then

Because the command (here the test-command or its equivalent [) will never see "file*" but only the list the shell creates from that and obviously in

Code:
if [ -r fileA fileB fileC ] ; then

after the workable "-r fileA" (is fileA readable?) there are two filenames test simply doesn't know what to do with.

Now, after this, what does eval do? It restarts the process of evaluating the command line so that the already done stages are done a second time. Where would one need that? For instance, when replacing variables with their content you can do:

Code:
var1="one"
var2="two"
x="1"

echo $var1

but you cannot do:

Code:
echo $var$x

because all variables are replaced at the same time and you would need to first replace "$x" with "1" and only then replace the resulting "$var1" with "one". You can do this with eval, though, because this way you get two stages of "variable replacement" and you can (with the help of escaping) put them in the necessary order:

Code:
eval echo \$var$x

The shell will do the following: the first "\$var" will not be expanded, because the escaped "$" will make it a ordinary character and hence "$var" is simply a string, not a variable. The "$x" is expanded, though, and replaced by a "1":

Code:
eval echo $var1

Now the "eval" makes the shell interpret it again and in the next variable replacement stage it will recognise the "$var1" as a now legal variable name and this time expand it:

Code:
echo one

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Eval

thank you (35 Replies)
Discussion started by: ratnalein88
35 Replies

2. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

3. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

5. Shell Programming and Scripting

echo file deleted by xargs

my command deletes the oldest file from a folder and I'd like to have some type of output when the file is selected or deleted. ls -t -1 | tail -n 1 | xargs rm I'm not sure how to incorporate echo into this. Thanks, (2 Replies)
Discussion started by: evanlivingston
2 Replies

6. UNIX for Dummies Questions & Answers

'echo dir_path | xargs cd' does not work?

Hi: how come this command does not work? the error message is: xargs: cd: No such file or directory yet, in the same time, 'echo dir_path | xargs ls' works. Does it work in bash? We use csh. Thanks. NB Phil (7 Replies)
Discussion started by: phil518
7 Replies

7. UNIX for Dummies Questions & Answers

Definition of "siblings" in /proc/cpuinfo

So, I'm looking over /proc/cpuinfo and have a question... I've read that "siblings" refers to hyperthreading, but that seems odd considering the contents of cpuinfo. Here's a part: model name : Intel(R) Xeon(R) CPU E5410 @ 2.33GHz physical id : 0 siblings : 4 core... (1 Reply)
Discussion started by: treesloth
1 Replies

8. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

9. Shell Programming and Scripting

EVal

Hi All, I'm running some encrypted data through a script I wrote. In order to do this, I'm using eval to resolve some of my variables. At the moment, when I use eval to resolve, it strips out some of my encrypted values, and totally drops some others. For example if I have the value ab1"3 it drops... (1 Reply)
Discussion started by: Khoomfire
1 Replies

10. Shell Programming and Scripting

using eval w/ echo

My script has the following for loop in it. foreach id (CLKM4 DGTM4 GNNM4) eval echo $id \$${id}sf \$${id}sfk end so "id" is a variable defined in the loop, and "idsf" and "idsfk" are variables defined in another part of the script, they return values of 0 or more. The problem i'm... (2 Replies)
Discussion started by: wxornot
2 Replies
Login or Register to Ask a Question