Reputation:
When I echo $PATH
I get: home/x/bin:home/x/.local/bin:...:...
and on and on where each path is separated by :
When I echo $PATH | tr ':' '\n'
I get:
home/x/bin
home/x/.local/bin
...
...
Where very path is separated by a new line
So now I want to fit all of this into a variable ($
) or a script so I can execute it and it lists the path by line, but it wont work:
I tried: x=${echo $PATH | tr ':' '\n'}
but I get -bash: ${echo $PATH | tr ':' '\n'}: bad substitution
I also tried a script where:
#!/bin/bash
echo $PATH > 11
sleep 1
bb=${`tr ':' '\n' < 11`}
echo $bb
What can I do?
Upvotes: 3
Views: 7995
Reputation: 2653
To expand @cyrus's Parameter Expansion idea, here's the alias version:
alias pathprint='echo "${PATH//:/$'"'"'\n'"'"'}"'
Need to use single quote '
around the aliased command to prevent $PATH
from being expanded while alias is defined.
Then, escape each single quote within command as:
' => ' + "'" + '
Stop previous quote '
, add a single quote using double quotes "'"
, followed by a single quote to resume quoting '
.
Upvotes: 2
Reputation: 2762
you should use command substitution. Curly Braces is use for grouping $ for variable substitution. while for command substition you need backticks if you se bourn shell, but for bash you need brackets
# Wrong
x=${echo $PATH | tr ':' '\n'}
# Right
x=$(echo $PATH | tr ':' '\n')
echo "$x"
Upvotes: 3