Cher
Cher

Reputation: 2937

how to pass an expression as a parameter?

Problem:

In a general way, I'm seeking a replicable way to do such things :

command expression_to_be_valuated

where expression_to_be_valuated would be any expression, which once executed would give, for example a number, so I would have

command parameter

where parameter is a number.

Current situation

I have the following expression

kill ps aux | grep '[m]ono ./' | awk '{print $2}'

Where if you refer to my explanation from above command is kill and expression_to_be_valuated is ps aux | grep '[m]ono ./' | awk '{print $2}'

I want this part to be evaluated before:

ps aux | grep '[m]ono ./' | awk '{print $2}'

However I tried to have '' around it (which to my understanding up to now meant "evaluate this expression before", normally it works but in this case I'm not able to make it works, or to call an eval on it, but it doesn't work.

Tries:

ps 'aux | grep '[m]ono ./' | awk '{print $2}''

Upvotes: 0

Views: 65

Answers (1)

fedorqui
fedorqui

Reputation: 289725

You can use command substitution and say ps $( command ):

ps $(aux | grep '[m]ono ./' | awk '{print $2}')

The $(command) expression evaluates command, so that if you say ps $(command), ps gets the output of command as input.

Note also that grep '[m]ono ./' | awk '{print $2}' can be squeezed into just awk '/mono .\// {print $2}'.


From the given link:

Command substitution allows the output of a command to replace the command itself.

Upvotes: 1

Related Questions