1

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?

2
  • 4
    PATH is a very important reserved variable. Because you're replacing its value in the second line, you whole script breaks. Commented May 17, 2018 at 15:05
  • @Robin479. That fixes it indeed. Can you post it as an answer so I can accept it? Commented May 17, 2018 at 15:09

1 Answer 1

2

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.

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

1 Comment

All all-uppercase variables are reserved for use by the shell.

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.