1

Assume that the Pid of active processes on my machines are 1000 and 2000. I am trying to make an array in Linux such that

The command echo ${Pid_Current[0]} gives 1000 in output

The command echo ${Pid_Current[1]} gives 2000 in output

Here is my code:

declare -a Pid_Current 

Pid_Current=$(ps -aF | tail -n +2 | awk '{print $2}')

However, instead of the desired output I explained above, I receive the following output:

echo ${Pid_Current[0]} gives 1000 2000 in output

echo ${Pid_Current[1]} gives nothing in output

Would you please advise me what part of my code is incorrect?

0

1 Answer 1

1

In bash array assignment is done by enclosing the expression in parenthesis, so to use array assignment you need to write:

Pid_Current=($(ps -aF | tail -n +2 | awk '{print $2}'))

Without parenthesis the result of the expression is assigned to Pid_Current[0]

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.