0

I have set of commands and I want all of them to be defined in a single variable. below are the commands I want display in output.

pwdx `ps -ef | grep java | cut -d' ' -f4` | cut -d/ -f7

I tried with

app=`pwdx `ps -ef | grep java | cut -d' ' -f4` | cut -d/ -f7` 
echo $app

but this gives empty output. Tried using for loop, that failed too.

Inian
  • 80,270
  • 14
  • 142
  • 161
Saikiran
  • 140
  • 14
  • can you try using () to those commands, Ex. $ (echo 111; echo 222; echo 333) which will give you an output 111 222 333. – Manjuboyz Feb 26 '20 at 03:17
  • Silly question, what do you see w/o the final cut? – tink Feb 26 '20 at 04:51
  • @eight Stacker: If you had really written the command **exactly** as you posted it, your assignment to `app` should have brought several errors, for instance _bash: -ef: command not found_ and a syntax error for the `|` in front of the _cut_. – user1934428 Feb 26 '20 at 07:46
  • @eightStacker : Also, I don't understand your question. What is your goal? What does, for instance, _I want display in output_ mean? I understand that you want to store into a variable a string which represents a series of commands. What do you want then to do with this string? – user1934428 Feb 26 '20 at 07:49

1 Answers1

3

Use the |& operator to pipe the output (stdout and stderr) of the previous command into the standard input of another one:

pwdx `ps -ef |& grep java |& cut -d' ' -f4` |& cut -d/ -f7

See this write-up for more on executing several bash commands: Running multiple commands in one line in shell

whoward3
  • 81
  • 4
  • Thanks for the reply mate, am trying to use a variable inside variable to execute it as a single command.. something like this... ``for i in `pwdx `ps -ef | grep java | cut -d' ' -f4` | cut -d/ -f7`; do echo $i ; done`` – Saikiran Feb 26 '20 at 03:22