This is a very quick post on how you can make a bash script that allows you to provide it values via the command line. Passing values to a bash script uses a 1-based array convention inside the script, that are referenced by prefixing with $ inside the script.

This means that if I provide .\ value1 value2, inside the I can retrieve these by referencing their positions:

echo $1 + $2

For improved clarity, you could assign them to new variables

echo $exp1 + $exp2

This is very simple usage and you can do a lot more sophisticated processing. I found this bash positional parameters page pretty handy and, of course, the next step would be to do named parameters.

You might like to play with these concepts on a bash Fiddle!

Here’s a real-world example. I wrote this a few months ago to install Azure File Storage drivers to Docker. It uses a parameter to be able to provide it the name of the config file holding Azure access credentials.

Quick tip: Passing values to a bash script
Tagged on:             

What do you think?