shell script : for loop to print all elements of PATH variable
I want to print individual elements of the PATH variable. This is what I tried :
Code:
for i in `$PATH | tr ':' ' '` Anyway, the code won't work because putting any thing in backquotes executes that statement too, hence "Not found" error will occur. How should I go about it ? |
Try:
Code:
for i in $(echo $PATH | tr ':' ' ') |
Thanks. But one thing. "-e" is getting printed in the output. But when I type
Code:
echo -e "hello\nthere" |
Well the first question would be, why are you using -e? If your code had the following:
Code:
echo "$i" Also, as an alternative, you could use parameter substitution instead of calling echo and tr: Code:
for i in ${PATH//:/ } |
Thanks for the alternate. And yeah,the -e switch was not required here. But still, if I DO use that, why is
Code:
echo -e "hello\nthere" |
echo $PATH|tr ':' ' ' -- to print in space separated manner
echo $PATH|tr ':' '\n' -- to print in the new line. Verified example ================ rs@rs-ThinkPad-T400:~$ echo $PATH|tr ':' ' ' /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games rs@rs-ThinkPad-T400:~$ echo $PATH|tr ':' '\n' /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games rs@rs-ThinkPad-T400:~$ Hope this is what you are looking for.. |
Quote:
Just for completeness' sake a version w/ sed ... Code:
echo $PATH | sed 's/:/\n/g' Tink |
Quote:
|
This will print all entries on individual lines:
Code:
Another option using an intermediate array: Code:
Quote:
|
Thanks everybody for so many solutions. I was using the command sh to execute the script. When I changed to bash, it worked just fine. Thanks again.
|
All times are GMT -5. The time now is 11:49 PM. |