1

If I define my archive folder it in my environment and export it, how do I access it in a shell script and run a program?

ARCHIVE=/home/kschmidt/public_html/CS265/Assignments/DrMath/Archive
export ARCHIVE
./prob1

Currently, my prob1 contains this code which I get an error when I try to run.

#!/bin/bash
print ARCHIVE

2 Answers 2

1

You expand a shell variable by prefixing it with a $, and print it with echo or printf command - shell doesn't have a print command:

echo "$ARCHIVE"

or

printf '%s\n' "$ARCHIVE"

As an aside, it is not good to use relative paths (as in ./prob1) in a script, unless you are explicitly cding to the directory where prob1 exists. So, either:

  • do an explicit cd to the script directory before invoking it with a relative path

or

  • use an absolute path (as in /path/to/prob1)

Related:

Sign up to request clarification or add additional context in comments.

Comments

0

prob1's code should be like:

#!/bin/bash
bash $ARCHIVE

then prob1 run like this:

bash prob1

I hope this help.

2 Comments

That is perfect thanks! One more quick question. How would I be able to loop through all the files in the directory and print them?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.