Score:0

How to change directories in a shell script?

mn flag

Background

I'm currently using the CLI for md-to-pdf. I'm not sure if it's by design or limitation, but when generating PDFs from MD files containing PNG hyperlinks from not within the directory containing the PNGs, the image URL is broken within the PDF.

If I run md-to-pdf within the directory containing the PNG & MD files, the PDF is generated which correctly embeds the PNG.

Workaround

As a workaround, I would like to change directory to the directory containing the PNG & MD files while the shell script is running (it's part of a pre-commit hook). If I run the shell script at the directory home/user/repo/, how do I CD to repo/docs/diagrams/<directoryWithPNG&MDs>? Sorry for the lack of better wording, but I'm new to shell scripting, I like it but it's tedious and I appreciate any help given.

Is it possible for me to return to the directory I was before I switched directories?

Thank you!

Doug Smythies avatar
gn flag
wouldn't it just be a `cd docs/diagrams/<directoryWithPNG&MDs>` line in the script? Otherwise show what you have done and the results, as it might give us better context.
nlecce avatar
mn flag
hi, i think you were correct. i was ambiguously using $(pwd). bonus question: without using `cd ../../..`, how would I traverse back up to `repo/`? is this possible? or must I use `cd ../../..`
Terrance avatar
id flag
Since it is `/home/user/repo` it would just be `~/repo`, However, you can also use `cd -` which will return to the last directory it was in before you changed to the other directory.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.