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.

4
  • can you try using () to those commands, Ex. $ (echo 111; echo 222; echo 333) which will give you an output 111 222 333. Commented Feb 26, 2020 at 3:17
  • Silly question, what do you see w/o the final cut? Commented Feb 26, 2020 at 4: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. Commented Feb 26, 2020 at 7: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? Commented Feb 26, 2020 at 7:49

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.