I'm trying to output my $PATH in the command line, but also replace the : with \n using the following:
echo $PATH | sed s/:/\n/g
This doesn't work 100%, how to I get it working?
You need to escape the backslash, i.e.
echo $PATH | sed s/:/\\n/g
Or, as BatchyX noted
echo $PATH | tr ':' '\n'
Another possibility, if you want to parse $PATH, you can also use
IFS=:
for i in $PATH; do
echo $i
done
Here IFS=:
tells bash
to split strings at :
. Please note, that this will alter other things in the current session, too. Excerpt from man bash
:
IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is
<space><tab><newline>
\n
as a whole represents the return character, isn't it? Escaping the backslash makes '\' and 'n' two separate characters
\
because the sed pattern is not quoted. A more robust method would be @user1936123 answer below.
You could also quote the regular expression:
echo $PATH | sed 's/:/\n/g'
For your \n literals, you could use echo -e
.
sed
? doesn'ttr ':' '\n'
work ?echo "$PATH" | sed 's/:/\n/g'
echo "${PATH//:/$'\n'}"