2

I'm trying to iterate through a list of folder names, and perform some operations on the name, but whatever I try to do inside the while loop, results in a "Command not found".

For example, the following code:

#!/bin/bash

C=$(echo "ABCDEF" | cut -c1)
R=$(echo "ABCDEF" | sed "s/A/X/g")
echo $C
echo $R

for PATH in $(find . -maxdepth 1 -type d); do
        C=$(echo $PATH | cut -c1)
        R=$(echo $PATH | sed "s/A/X/g")
        echo $C
done

Outputs:

A
XBCDEF
line 9: cut: command not found
line 10: sed: command not found

1 Answer 1

6

PATH is a special variable that tells the shell where to find common utilities. For instance, sed and cut are usually in /bin and $PATH usually includes /bin.

So, in your for loop, you've redefined $PATH to be the result of your find operation. You'll have better luck if you use a variable name other than PATH.

Sign up to request clarification or add additional context in comments.

1 Comment

best practice is to use lowercase variable names for exactly this reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.