O'Reilly logo

Shell Scripting: Expert Recipes for Linux, Bash, and More by Steve Parker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Preset and Standard Variables

There are a few variables which are provided by the shell itself. These vary in nature quite a bit: Some of them are purely informational, such as $BASH_VERSION, which tells you the version of bash that you are running (for example, 4.1.5(1)-release), but which you can override simply by setting them. Others return a different value depending on the current environment, such as $PIPESTATUS, which tells you the return codes of the commands in the last-run pipeline, and still others return a different value depending on some function of their own (such as $SECONDS, an integer which goes up by one every second). Also, some of these variables lose their special meaning if you set (or unset) them: $RANDOM will keep returning random numbers until you assign it a value, after which it will not produce random numbers again. $SECONDS will always keep counting; if you set SECONDS=35 and then read $SECONDS again 5 seconds later, it will have counted up to 40. Still others are not writeable at all; you will get an error message if you try setting UID, for instance. There are yet others, like TIMEFORMAT, which do nothing by themselves, but are used to affect how other commands work.

The following examples include some of the more common and useful predefined variables, as well as some that are less well known.

BASH_ENV

BASH_ENV is the name of a file (which may be a relative path, in which case be very careful about where you run your scripts from!) which is parsed ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required