Use sed on a string variable rather than a file
This has nothing to do with sed
as such, it applies to any program that reads standard input. Anyway, what you were looking for is
sed 's/:/ /g' <<<"$PATH"
There's no reason to save in another variable, nothing you do this way will affect the value stored in the variable. You need the assignment operator (=
) for that. The syntax used above is called a "here string" and is specific to bash, ksh and zsh. It serves to pass a variable's value as input to a program that reads from standard input.
I have written up an answer on U&L that lists all the various shell operators like this one. You might want to have a look.
Note that you could also have done
echo "$PATH" | sed 's/:/ /g'
Finally, you really don't need sed
at all for this. You can do the whole thing in bash directly:
echo "${PATH//:/ }"
The above is a bash replacement construct. Given a variable $var
, a pattern pat
and a replacement (rep
), to replace all occurrences of pat
with rep
, you would do
echo "${var//pat/rep}"
The same thing with a single slash replaces only the first occurrence:
echo "${var/pat/rep}"
May be:
~$ x="$PATH"
~$ x="$(echo $x | sed 's/:/ /g')"
~$ echo "$x"
/usr/local/bin /usr/bin /bin /usr/local/games /usr/games