1

I know I should be able to change the current working directory of a bash script by doing something akin to

cd `dirname $MYPATH`

but for some reason this doesn't work (or not as I imagined it).

#!/bin/bash

WAYPATH="/home/user/articles"
TEST_PATH="/home/user/testing"


# Set working directory of the script to be testing
cd `dirname $TEST_PATH`

for i in $(ls $WAYPATH); do

another_command $i $i.r > $TEST_PATH/htmls/$i.html

done

My goal here is to allow the bash script to find the files located in TEST_PATH (which have matching name to those in WAY_PATH) without having to prefix them with the full path (because another_command) makes use of the whole argument passed to it.

Three Diag
  • 585
  • 2
  • 7
  • 21
  • 1
    `cd $TEST_PATH` – Ipor Sircer Nov 20 '16 at 13:11
  • Thank you, this works. I am puzzled by the different solutions proposed here then: http://stackoverflow.com/questions/3349105/how-to-set-current-working-directory-to-the-directory-of-the-script . Why does the `$0` variable require dirname? Oh, I see `man dirname`! – Three Diag Nov 20 '16 at 13:16

1 Answers1

0

So this is a lesson on understanding what commands do after reading about them on Stackexchange. I was using

cd `dirname $MYPATH`

following this answer where they achieved the desired result

cd `dirname $0`

$0 is the full path of the bash script, so dirname is required to return the path without the name of the file.

Instead, for an arbitrary supplied path is sufficient to do a simple

cd $MYPATH

as suggested in comments.

Community
  • 1
  • 1
Three Diag
  • 585
  • 2
  • 7
  • 21