Apple Intelligence is now available on iPhone, iPad, and Mac!

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How use breakpath command in shell file?

From somewhere I have available in the macOS 10.15.1 Terminal window the command "breakpath", which gives as output the entries in $PATH, one per line. And I can redirect its output, e.g.:


    breakpath > ~/myPATH.txt


If I use it in a bash shell file "~/printpath.sh" such as


    #! /bin/bash
    breakpath > ~/myPATH.txt


however, I get an empty file written and, in Terminal, the error message:


    ./printpath.sh: line 2: breakpath: command not found


What's wrong?


Unfortunately, as I said, I don't know the source of "breakpath". I tried "man breakpath" but get "No manual entry for breakpath". And when I search my Mac for a file named "breakpath", I find nothing.

iMac 27", macOS 10.15

Posted on Dec 10, 2019 9:15 AM

Reply
Question marked as Top-ranking reply

Posted on Dec 10, 2019 12:10 PM

Found it, in my `.profile`:


alias breakpath="tr ':' '\n' <<< \"$PATH\""


Is the take-away that the bash shell just does not know such alias commands from my `.profile`? As I said, what happens is I get an empty file written.


However, if I use in the shell file the line


tr ':' '\n' <<< \"$PATH\" 


or even the more direct line


echo "${PATH//:/$'\n'}" > ~/myPATH.txt 


in the shell file, then it works OK.


Similar questions

4 replies
Question marked as Top-ranking reply

Dec 10, 2019 12:10 PM in response to VikingOSX

Found it, in my `.profile`:


alias breakpath="tr ':' '\n' <<< \"$PATH\""


Is the take-away that the bash shell just does not know such alias commands from my `.profile`? As I said, what happens is I get an empty file written.


However, if I use in the shell file the line


tr ':' '\n' <<< \"$PATH\" 


or even the more direct line


echo "${PATH//:/$'\n'}" > ~/myPATH.txt 


in the shell file, then it works OK.


Dec 10, 2019 12:41 PM in response to murrayE

Sorry, somehow I read Bash and my brain interpreted as Zsh, so my Zsh only contribution won't work for you in Bash.


Your Bash ~/.profile is the last dot file to be searched for by Bash when you launch a new Terminal window. After /etc/profile, it reads your ~/.bash_profile, ~/.bash_login, and then the ~/.profile file. I put my aliases in ~/.bashrc and source that file in my ~/.bash_profile.


You can drop the escaped, or even the double-quotes in the following and it will still work:


tr ':' '\n' <<<$PATH



Dec 10, 2019 2:50 PM in response to murrayE

This is the code that I use in my profile.


# List the items in a PATH, CLASSPATH, or similar variable


list_items_in_path() {


   OLDIFS=${IFS}


   IFS=":"


   INPUT=""


   eval INPUT=\$${1}


   for item in ${INPUT} 


   do


   echo ${item}


   if  [ ! -f ${item} ] && [ ! -d ${item} ]  


   then


   echo " *****  ${item} does not exist"


   fi


   done


   IFS=${OLDIFS}


}


When you start mixing the rules by using aliases in shell scripts. I have often found the results hard to understand. However, my first thought would be to see what the alias command shows. Sometimes you need an extra layer of applying encoding or decoding. When I executed the alias command from your script into my script, typing breakpath listed the items in the path. I'll have to look at this in more detail. However, I have found that running aliases in scripts or scripts in aliases can get very confusing.



How use breakpath command in shell file?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.