A
A
AIIVY ⠀2020-10-04 21:47:30
bash
AIIVY ⠀, 2020-10-04 21:47:30

How to change spaces to %20 in bash script?

I am writing a bash script that accepts a request via read, inserts it into a link and opens this link. You need to make sure that if the request contains spaces, then they are automatically changed to %20, since %20 is the space code. How to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Dobryshin, 2020-10-04
@FryGuy

For example like this:

LINE="test test test"
LINE=$(echo $LINE | sed "s/ /%20/g")
echo $LINE

X
xotkot, 2020-10-04
@xotkot

if in the future it is supposed to transfer this data via a URL, then replacing only spaces will obviously not be enough, for example, I use the jq
utility for this example:

echo 'This works if x < y' | jq -Rr '@uri'
This%20works%20if%20x%20%3C%20y

in your case it is:
read query
echo "$query" | jq -Rr '@uri'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question