2

I'm trying to write a shell script which basically goes into a particular folder and performs some actions. The catch here is that the folder name is variable i.e. /path/to/variable

I was thinking, is it possible to run a shell script from command line and also define a variable in the same line? Something like:

./run.sh $id=456

When it runs, it takes the path as /path/to/$id and hence /path/to/456.

2 Answers 2

4

You can run the script in the following style:

./run.sh 465

Than you can access the content of the parameter inside the script with $1 (1st parameter), $2 (2nd parameter) and so on. To change the directory use cd /path/to/$1 BTW: $# returns the count of parameters.

Another way is to use arguments, like here: http://www.linux.com/archive/feature/118031 So you can run your script like this: ./run.sh -d 465 (where -d stand for directory)

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

1 Comment

...because it was in easy question? Does the answer fits your needs?
3

Other way around.

id=456 ./run.sh

1 Comment

Maybe it is worth explaining that this automatically exports the variable id with the value 456 to the script run.sh (and only to that script and any programs it runs; not to anything run after it).

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.