LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script : for loop to print all elements of PATH variable (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-for-loop-to-print-all-elements-of-path-variable-885773/)

daudiam 06-11-2011 01:00 PM

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 ':' ' '`
do
        echo -e "$i\n"
done

I changed the colon separators to spaces so that the for statement can recognize the different elements of the PATH variable (Is there any way that for can actually recognize colon as the delimiter ?)

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 ?

sycamorex 06-11-2011 01:20 PM

Try:
Code:

for  i in $(echo $PATH | tr ':' ' ')
do
        echo -e "$i\n"
done

Note that the use of $(....) is preferred over backticks.

daudiam 06-11-2011 01:49 PM

Thanks. But one thing. "-e" is getting printed in the output. But when I type
Code:

echo -e "hello\nthere"
on the terminal, it doesn't display "-e". Why is that so ?

grail 06-12-2011 01:02 AM

Well the first question would be, why are you using -e? If your code had the following:
Code:

echo "$i"
A new line will be appended anyway.

Also, as an alternative, you could use parameter substitution instead of calling echo and tr:
Code:

for i in ${PATH//:/ }
do
    echo "$i"
done


daudiam 06-12-2011 01:12 AM

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"
exhibiting different behaviours when executed in a shell script and when executed on the command line ?

ssrameez 06-12-2011 01:27 AM

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..

Tinkster 06-12-2011 01:29 AM

Quote:

Originally Posted by daudiam (Post 4382790)
I want to print individual elements of the PATH variable. This is what I tried :
Code:

for  i in `$PATH | tr ':' ' '`
do
        echo -e "$i\n"
done

I changed the colon separators to spaces so that the for statement can recognize the different elements of the PATH variable (Is there any way that for can actually recognize colon as the delimiter ?)

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 ?


Just for completeness' sake a version w/ sed ...
Code:

echo $PATH | sed 's/:/\n/g'
Cheers,
Tink

catkin 06-12-2011 01:30 AM

Quote:

Originally Posted by daudiam (Post 4383098)
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"
exhibiting different behaviours when executed in a shell script and when executed on the command line ?

Could be different shells. How is the script being run? If the first line of the script begins with #! what is it?

David the H. 06-12-2011 01:44 AM

This will print all entries on individual lines:

Code:


echo "${PATH//:/$'\n'}"

Uses simple parameter substitution and ansi-style quoting. The extquote shell option needs to be enabled in order for it to be used inside expansions. This is the default in interactive shells, but not in scripts. The expansion also needs to be quoted to preserve the newlines, of course.

Another option using an intermediate array:
Code:


IFS=":"
pathlist=( $PATH )
IFS=$'\n'
echo "${pathlist[*]}"
unset IFS

${array[*]} prints a string of all individual array elements, separated by the first character of IFS.

Quote:

why is
Code:

echo -e "hello\nthere"
exhibiting different behaviours when executed in a shell script and when executed on the command line ?
Most probably your system is using dash or another shell when interpreting basic shell scripts, and it has a different set of options for echo. Change your shebang from #!/bin/sh to #!/bin/bash and see what happens.

daudiam 06-12-2011 02:57 AM

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.