Angad
Angad

Reputation: 84

Defining a variable when running a shell script?

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.

Upvotes: 2

Views: 175

Answers (2)

The Bndr
The Bndr

Reputation: 13394

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)

Upvotes: 4

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

Other way around.

id=456 ./run.sh

Upvotes: 3

Related Questions