Frank
Frank

Reputation: 2706

Command not found in bash script

I have a folder containing some files including ".bar" files. Now I would like to make a bash script that finds these ".bar" files and creates a folder with the same name. However I can't seem to get the syntax right.

I use:

#!/bin/bash
PATH="folder"

for filename in ${PATH}/*.bar; do
    mkdir $(basename ${filename%.*})
done

It seems to work when I use it in a terminal, but when I put in in a script it fails with the error: "basename: command not found" and "mkdir: command not found". How can I get this to work?

Upvotes: 1

Views: 1678

Answers (1)

Eby Jacob
Eby Jacob

Reputation: 1458

try this

#!/bin/bash
folderpath="folder"

for filename in "${folderpath}"/*.bar; do
    mkdir "$(basename "${filename%.*}")"
done

PATH is an internal shell variable and shouldn't be used in your shell script.

Upvotes: 2

Related Questions