bash

How run a bash script in it’s directory

Posted by admin on November 18, 2017
linux / 3,350 Comments

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.

[bash]
# 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}”
done
[/bash]

I hope this little script be useful to you.

Best,
L.

Tags: , ,