0

I was in a case where I deleted all the debugs in my code but still got outputs in my terminal when executing my script. All of this is simply after trying to implement a replace on a string.

2 Answers 2

1

In this case, you need to set the value of the variable to the desired value in which you are making replacements. The .replace method is only returning a value to the host and will not set the variable which is why your second command is appropriate.

In other situations, you suppress output by piping it to the Out-Null cmdlet like so:

$url.replace('>', ' ') | Out-Null

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

1 Comment

Yes, this works too ! The thing is that I didn't knew there will be an output, so I was very confused after seeing some in my terminal. But if you already know about ther output and only want to hide it, your solution is the best ! Thanks
0

I just wanted to replaces some chars with spaces in a list of URLs. Here was the code :

$url.replace('>', ' ')

Doing it this way, I got a lot of outputs in my terminal, showing each replaced URLs without me asking for any outputs !

But as you can see here, there is a miss... I'm not assigning the return of this replace. The line should have been :

$url = $url.replace('>', ' ')

And it looks that after assigning it, all the outputs have disappeared.

I don't understand the fundamentals of this behaviour but according to the documentation, this looks actually normal.

As simple as it is, I hope this will save some search time.

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.