Tony Han
Tony Han

Reputation: 2230

How to add the version function when writing my own shell script?

I'm trying to write my own shell script. It's almost done, so I want to add the version. For example, foo -v to print foo 1.0.0.

But I'm not sure what's a good way. I can write the version in my execute file but I have to change that each time I update the program to a new version.

I need your help :)

Upvotes: 1

Views: 180

Answers (1)

thb
thb

Reputation: 14454

Since you have the version number somewhere (I mean, somewhere on the computer, somewhere other than in your head), you can keep the number there (preferably, if practicable, in only one place), then read that and output.

There are of course a lot of ways to do this. One way is by the shell's . sourcing mechanism. For example, if the file foo_version consists of the single line

FOO_VERSION=1.0.0

then you can use this in foo by the likes of

#!/bin/bash -e
. $(dirname "$0")/foo_version

# ...

echo "foo ${FOO_VERSION}"

Or your foo_version can just consist of 1.0.0, in which case your foo would simply read it as text. And there are yet further possibilities, as well, especially if you will package your software for distribution.

As @JonathanLeffler observes, a more careful approach is necessary when you work with a team and/or are using version-control software (RCS, CVS, Subversion, Mercurial, Git, etc.), but if and when you start using version-control software, when you read the version-control software's manual, you will soon learn all about that.

Upvotes: 1

Related Questions