How run a bash script in it’s directory

Posted by admin on November 18, 2017

To run a script in it’s directory and not in the directory we’re calling it, we can use the dirname command, pass their result to a variable, cd to the path and then execute the tasks.

# Here we pass the dirname to a variable
dirname=`dirname $0`

# Change current directori
cd $dirname

# Execute the task, in this case, copy html files and save them as php files
for file in ./*.html; do
#cp ./mondial.* ./"$file"
cp $file "${file/html/php}"

I hope this little script be useful to you.


Tags: , ,