[SOLVED] shell script : for loop to print all elements of PATH variable
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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 ?
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 ?
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.
${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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.