LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-11-2011, 01:00 PM   #1
daudiam
Member
 
Registered: Mar 2010
Posts: 68

Rep: Reputation: 15
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 ?
 
Old 06-11-2011, 01:20 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Try:
Code:
for  i in $(echo $PATH | tr ':' ' ')
do
        echo -e "$i\n"
done
Note that the use of $(....) is preferred over backticks.
 
Old 06-11-2011, 01:49 PM   #3
daudiam
Member
 
Registered: Mar 2010
Posts: 68

Original Poster
Rep: Reputation: 15
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 ?
 
Old 06-12-2011, 01:02 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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
 
Old 06-12-2011, 01:12 AM   #5
daudiam
Member
 
Registered: Mar 2010
Posts: 68

Original Poster
Rep: Reputation: 15
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 ?
 
Old 06-12-2011, 01:27 AM   #6
ssrameez
Member
 
Registered: Oct 2006
Location: bangalore
Distribution: Fedora, Ubuntu, Debian, Redhat
Posts: 82

Rep: Reputation: 6
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..
 
Old 06-12-2011, 01:29 AM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by daudiam View Post
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
 
Old 06-12-2011, 01:30 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by daudiam View Post
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?
 
Old 06-12-2011, 01:44 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 06-12-2011, 02:57 AM   #10
daudiam
Member
 
Registered: Mar 2010
Posts: 68

Original Poster
Rep: Reputation: 15
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.
 
  


Reply

Tags
loop, script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [bash] while loop ..always print the first variable. ridwanfi Programming 5 06-10-2011 08:12 AM
[SOLVED] Shell Script - Use variable in a for loop with directory path Tech109 Linux - General 2 01-19-2011 10:22 AM
bash script path issue - how to pass a path as a string to a variable PiNPOiNT Programming 5 04-17-2009 05:48 PM
shell script question $variable in loop icecubeflower Linux - Newbie 2 03-31-2009 09:09 AM
Trying to write a perl script that will print shell variable ohcarol Programming 2 04-16-2007 08:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration