-1

I have 2 bash scripts:

tmp/a.sh

#!/bin/bash
cd frontend
yarn install
yarn build

docker-compose build
docker-compose up -d

b.sh

#!/bin/bash
./tmp/a.sh

I want to launch a.sh from b.sh but when ls runs, it prints directory of b.sh(parent process). How do I preserve its path when a.sh runs?

J.S.C
  • 1,323
  • 3
  • 16
  • 22
  • 1
    In `b` you call `a` which simply does `ls`. It is equivalent to simply having `b` contain `ls` -- there is no path information present and no changing of directories -- so it is naturally just listing the `$PWD`. What directory do you want it to list and how are you providing the path information to `a` if `b` is executed from another directory? You need to provide the *absolute path* to `a` in `b` and simply do `/path/to/a.sh "$PWD"` in `b`. (or something similar --depending on what you want to accomplish -- which isn't entirely clear) – David C. Rankin Aug 11 '20 at 02:09
  • @DavidC.Rankin I am building a program that will be deployed to different environments so I don't want to use absolute path because it can change(Note that `a.sh` is in subdirectory of `b.sh`). What I am actually doing in `a.sh` is `cd` into its subdirectory and running `yarn start`. but because `a.sh` is launched from `b.sh`, it tries to find subdirectory from `b.sh`'s path which doesn't exist – J.S.C Aug 11 '20 at 02:29
  • Please edit the question and show what you're really doing. Because right now the question makes no sense, since there's no `cd`. – Barmar Aug 11 '20 at 02:39
  • if `a.sh` always changes into a subdirectory, it can do `ls ..` – Barmar Aug 11 '20 at 02:41
  • I've updated `a.sh`. when `a.sh` is launched, it fails to `cd` and `yarn install` because it is looking for files from `b.sh` directory. Thank! – J.S.C Aug 11 '20 at 03:25

1 Answers1

1

I think, this old thread holds an answer to your question.

The accepted answer on the thread says modify a.sh to the following:

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ls $DIR

or

#!/bin/bash
ls $( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )
ZYX Rhythm
  • 73
  • 7