-2

I have a number of bash scripts that use the following variables. Would it make sense to include them an Environment Variables? If so, how can I declare them properly?

wht="$( tput bold; tput setaf 15 )"
grn="$( tput bold; tput setaf 34 )"
blu="$( tput bold; tput setaf 39 )"
ylw="$( tput bold; tput setaf 11 )"
red="$( tput bold; tput setaf 196 )"
amb="$( tput bold; tput setaf 214 )"

cyn="$( tput bold; tput setaf 51 )"
mgn="$( tput bold; tput setaf 201 )"
3
  • 1
    I expect this will get closed as "opinion based". However my opinion is "no". If you're only ever going to use it yourself then maybe, but one day you'll copy the script to another machine and it'll break. If you ever want to share the script with other people and then you don't want unnecessary external dependencies (all those variables). Just don't do it :-) Mar 13 at 1:18
  • I've done a whole bunch of stuff like this over the years, adding utility variables and functions to my environment only to find after a short time I never use them again. In the long term I'd rather recommend bookmarking (or otherwise saving a reference to) the documentation, since that's going to stay relevant long after you forgot that these variables existed.
    – l0b0
    Mar 13 at 1:27
  • One possibility is to declare them at the beginning of a bash file (rather than locally within bash functions) and then source that file at the start.
    – konmi
    Mar 13 at 1:45

1 Answer 1

0

There are two ways to do it, depending on your actual use of them.

  1. Add the definitions into your ~/.bashrc or call the script with those definitions from ~/.bashrc. This is useful for any scripts you write for yourself. And even if you type a command right here and now... As a variant, you can put the definitions globally, into /etc/bashrc and any user on the system can use these variables. Of course, you would need to add an export:
export wht="$( tput bold; tput setaf 15 )"
export grn="$( tput bold; tput setaf 34 )"
...
  1. Put the definitions into a /usr/local/bin/colors.sh (ensure that /usr/local/bin is in your $PATH) and any script you write after that - just add source colors.sh. The colors.sh in this case acts like a library of shell functions/definitions. This approach is useful for cases where you write scripts for some suit which can be installed on a different machine.

So, first decide how and where you would use these environment variables. It will give you an answer on where to store them.

1
  • I am working on a library with tools for installing on different machines. Will do the source way,
    – konmi
    Mar 13 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.