#!/bin/bash # 1st part ret=$(ps aux | grep -v grep) # thats OK echo $ret # 2nd part cmd="ps aux | grep -v grep" # a problem with the pipe | ret=$($cmd) echo $ret
How can I use the command line, as I have in the second part? Think the problem is in the pipe. Tried to run away, but it didnโt help. Get some snytax ps error.
Thanks!
Using eval is not recommended here. This can lead to unexpected results, especially when variables can be read from unreliable sources (see BashFAQ / 048 - Accounting team and security issues .
eval
You can solve this in a simple way by defining and naming a function below
ps_cmd() { ps aux | grep -v grep }
and use it in a script like
output="$(ps_cmd)" echo "$output"
Also, a good reading would be to find out why storing commands in a variable is not a good idea and has many potential errors - BashFAQ / 050 - I try to enter a command into a variable, but difficult cases always fail!
You need eval :
ret=$(eval "$cmd")
Source: https://habr.com/ru/post/1274505/More articles:ngnix passes data to the unix unix domain completely - linuxCloud Firestore Case insensitive query sorting - javascripthttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1274502/how-can-i-find-out-which-cache-line-is-touched-by-an-instruction-on-an-intel-processor&usg=ALkJrhjym3JXNvz26TlcyH3x-wV9zAsDkQOpenGL Errors for Android SurfaceView - androidApache Flask 13 error, permission denied - pythonAndroid Wearable.API is deprecated. What should i use instead? - androidbuilt-in cosine smoothing function in matlab - matrixHow to order one list by index values โโstored in another list - javaJava Complexity, Collectors.toList () - time-complexityto import a package that does not exist - pythonAll Articles